Created
June 30, 2016 15:47
-
-
Save samr28/3e9611bd2cfadcca8a0aba8cb2edd1ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <EEPROM.h> | |
LiquidCrystal lcd(12,11,5,4,3,2); | |
const int LCD_WIDTH = 16, LCD_ROWS = 2; | |
const int motorPin = 9; | |
const int DISPLAY_PWM = 6; | |
int DISPLAY_CONTRAST = 90; | |
const int ON_BUTTON_PIN = 7, OFF_BUTTON_PIN = 8, EXTRA_BUTTON_PIN = 13; | |
const int POT1_PIN = 0; | |
const int EEPROM_ADDRESS_LCD = 1; | |
const int UPDATE_TIMER = 700; | |
bool onButton = false; | |
bool offButton = false; | |
bool extraButton = false; | |
bool turnedOffRecently = false; | |
int pot1Value = 0; | |
int motorSpeed = 0; | |
bool isOn = false; | |
void setup() | |
{ | |
DISPLAY_CONTRAST = EEPROM.read(EEPROM_ADDRESS_LCD); | |
analogWrite(DISPLAY_PWM, DISPLAY_CONTRAST); | |
lcd.begin(16,2); | |
lcd.clear(); | |
lcd.print(" Power: Off"); | |
loadingBar(); | |
pinMode(ON_BUTTON_PIN, INPUT); | |
pinMode(OFF_BUTTON_PIN, INPUT); | |
pinMode(POT1_PIN, INPUT); | |
pinMode(motorPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
onButton = digitalRead(ON_BUTTON_PIN); | |
offButton = digitalRead(OFF_BUTTON_PIN); | |
extraButton = digitalRead(EXTRA_BUTTON_PIN); | |
pot1Value = analogRead(POT1_PIN); | |
//Serial.println(extraButton); | |
if (onButton && !isOn) { | |
Serial.println("POWER ON"); | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(" Power: On"); | |
lcd.setCursor(0,1); | |
lcd.print("Motor Speed:"); | |
isOn = true; | |
} | |
if (offButton && isOn) { | |
analogWrite(motorPin, 0); | |
isOn = false; | |
} | |
if (extraButton) { | |
lcd.clear(); | |
Serial.println("Open Settings"); | |
lcd.setCursor(0,0); | |
lcd.print("Settings: LCD"); | |
lcd.setCursor(0,1); | |
lcd.print("Contrast: "); | |
lcd.print(DISPLAY_CONTRAST); | |
delay(500); | |
//lcd.clear(); | |
extraButton = false; | |
Serial.println(extraButton); | |
while (extraButton == false) { | |
Serial.println(extraButton); | |
extraButton = digitalRead(EXTRA_BUTTON_PIN); | |
pot1Value = analogRead(POT1_PIN); | |
//DISPLAY_CONTRAST = map(pot1Value, 0, 1023, 0, 2000); | |
//DISPLAY_CONTRAST = constrain(pot1Value, 0, 2000); | |
DISPLAY_CONTRAST = pot1Value / 4; | |
analogWrite(DISPLAY_PWM, DISPLAY_CONTRAST); | |
lcd.setCursor(0,1); | |
lcd.print("Contrast: "); | |
lcd.print(DISPLAY_CONTRAST); | |
} | |
EEPROM.write(EEPROM_ADDRESS_LCD, (pot1Value/4)); | |
Serial.println(extraButton); | |
delay(500); | |
extraButton = false; | |
turnedOffRecently = false; | |
} | |
if (isOn) { | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(" Power: On"); | |
lcd.setCursor(0,1); | |
lcd.print("Motor Speed:"); | |
lcd.print(motorSpeed); | |
delay(UPDATE_TIMER); | |
turnedOffRecently = false; | |
motorSpeed = map(pot1Value, 0, 1023, 0, 255); | |
motorSpeed = constrain(motorSpeed, 0, 255); | |
analogWrite(motorPin, motorSpeed); | |
} | |
else if (!turnedOffRecently) { | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(" Power: Off"); | |
analogWrite(motorPin, 0); | |
turnedOffRecently = true; | |
} | |
} | |
void loadingBar() { | |
turnedOffRecently = false; | |
Serial.println("LOADING"); | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print(" Loading..."); | |
lcd.setCursor(0,1); | |
lcd.print("["); | |
lcd.setCursor(15,1); | |
lcd.print("]"); | |
String load = "="; | |
int i = 1; | |
while (i <= 14) { | |
lcd.setCursor(i-1,1); | |
if (i == 1) { | |
lcd.setCursor(1,1); | |
} | |
lcd.print(load); | |
lcd.setCursor(i,1); | |
lcd.print(">"); | |
delay(random(300,2000)); | |
i++; | |
} | |
lcd.print("]"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment