Hi,
I'm pretty new to programming and arduino, however for a bit of intrigue, i decided to build an aquarium led controller and it seems like i finally managed to finish my 1st working version of it.
The idea is to control 3 led colors (3 PWM outputs) through LCD+keypad, with a view to adding timers and other random stuff.
The idea of the project is that i have 4 pages of menu that follow one another linearly (mainscree-whitescreen-bluescreen-redscreen-savescreen). In the (color)screens i can increase/decrease the values with the builtin Sainsmart 16x2 lcd shield buttons. However i've noticed that the more "if's" i add, the slower the adjustment of values becomes (for example, when there's only 1 color menu, the values just fly up or down when you hold the up/down button). Perhaps there's some fundamental mistake i'm making that'd improve the speed?
Another thing is that when i press a button to navigate through menu (RIGHT key), it often jumps past one menu page. Does it have something to do with debouncing ?
#include <LiquidCrystal.h>
#include <DFR_Key.h>
#include <EEPROM.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
// #define ONE_WIRE_BUS 2
// #define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
// OneWire oneWire(ONE_WIRE_BUS);
//OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
// Pass our oneWire reference to Dallas Temperature.
// DallasTemperature sensors(&oneWire);
//Pin assignments for SainSmart LCD Keypad Shield
LiquidCrystal lcd(8, 9, 4, 12, 13, 7);
//---------------------------------------------
// Keypad
DFR_Key keypad;
int localKey = 0;
String keyString = "";
// EEPROM
int whiteAddress = 1;
int blueAddress = 2;
int redAddress = 3;
// Leds
int whitePower;
int bluePower;
int redPower;
float totalWats;
float whiteWats;
float blueWats;
float redWats;
int whitePin = 3;
int bluePin = 5;
int redPin = 6;
// Menus
int menu = 1;
/// Symbols ///
/* byte wave[8] = {
B00000,
B00000,
B01000,
B11101,
B10111,
B00010,
B00000,
};
*/
byte lamp[8] = {
B11111,
B01110,
B00000,
B00000,
B01010,
B00000,
B10001,
};
/////////////START KEYPAD///////////
void setup()
{
lcd.createChar(0,lamp);
whitePower = EEPROM.read (1);
bluePower = EEPROM.read (2);
redPower = EEPROM.read (3);
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.write (byte(0));
lcd.print ("Uber AQUA LEDs");
lcd.write (byte(0));
delay (2000);
lcd.clear();
Serial.begin(9600);
pinMode (whitePin, OUTPUT);
pinMode (bluePin, OUTPUT);
pinMode (redPin, OUTPUT);
/*
OPTIONAL
keypad.setRate(x);
Sets the sample rate at once every x milliseconds.
Default: 10ms
*/
keypad.setRate(1);
}
void loop()
{
analogWrite (whitePin, whitePower);
analogWrite (bluePin, bluePower);
analogWrite (redPin, redPower);
whiteWats = 42 * 3 * (whitePower/255.0);
blueWats = 9 * 3 * (bluePower/255.0);
redWats = 9 * 3 * (redPower/255.0);
totalWats = whiteWats+blueWats+redWats;
localKey = keypad.getKey();
Serial.print (whiteWats);
Serial.print (" / ");
Serial.print (blueWats);
Serial.print (" / ");
Serial.print (redWats);
Serial.print (" / ");
Serial.println (totalWats);
if (menu == 1) {
mainscreen ();
}
if (menu == 2) {
whitescreen ();
}
if (menu == 3) {
bluescreen ();
}
if (menu == 4) {
redscreen ();
}
if (menu == 5) {
savescreen ();
}
/////////////////////// SWITCH LOOP START//////
if (menu == 1 && localKey != SAMPLE_WAIT){ // If on mainmenu, move to whitescreen() if RIGHT button pressed
switch (localKey) {
case RIGHT_KEY:
menu = 2;
return;
}
}
if (menu == 2 && localKey != SAMPLE_WAIT){ // if on whitescreen (), increase/decrease whitePower if UP/DOWN pressed and move to bluescreen() if RIGHT button pressed
switch (localKey) {
case UP_KEY:
if (whitePower <255) {
whitePower=whitePower+1;
}
break;
case DOWN_KEY:
if (whitePower >0){
whitePower=whitePower-1;
}
break;
case RIGHT_KEY:
menu = 3;
return;
}
}
if (menu == 3 && localKey != SAMPLE_WAIT){ // if on bluescreen (), increase/decrease bluePower if UP/DOWN pressed and move to redscreen() if RIGHT button pressed
switch (localKey) {
case UP_KEY:
if (bluePower <255) {
bluePower=bluePower+1;
}
break;
case DOWN_KEY:
if (bluePower >0){
bluePower=bluePower-1;
}
break;
case RIGHT_KEY:
menu = 4;
return;
}
}
if (menu == 4 && localKey != SAMPLE_WAIT){ // if on redscreen (), increase/decrease whitePower if UP/DOWN pressed and move to savescreen() if RIGHT button pressed
switch (localKey) {
case UP_KEY:
if (redPower <255) {
redPower=redPower+1;
}
break;
case DOWN_KEY:
if (redPower >0){
redPower=redPower-1;
}
break;
case RIGHT_KEY:
menu = 5;
lcd.clear();
return;
}
}
if (menu == 5 && localKey != SAMPLE_WAIT){ // if on savescreen (), save settings if SELECT button pressed, move to mainmenu () if RIGHT button pressed
switch (localKey) {
case RIGHT_KEY:
lcd.clear ();
menu = 1;
return;
case SELECT_KEY:
EEPROM.update(whiteAddress, whitePower);
EEPROM.update(blueAddress, bluePower);
EEPROM.update(redAddress, redPower);
lcd.clear();
lcd.setCursor (6,0);
lcd.print ("DONE");
delay (2000);
lcd.clear();
menu = 1;
return;
}
}
}
// SCREENS: Main->white->blue->red->savescreen
void mainscreen() {
lcd.setCursor (0,0);
lcd.print ("W:");
lcd.print (map(whitePower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (9,0);
lcd.print ("B:");
lcd.print (map (bluePower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (0,1);
lcd.print ("R:");
lcd.print (map (redPower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (9,1);
lcd.write (byte(0));
lcd.print (totalWats,0);
lcd.print ("W");
lcd.setCursor (15,1);
lcd.print (">");
}
void whitescreen() {
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("White: ");
lcd.print (map(whitePower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (14,1);
lcd.print ("->");
}
void bluescreen() {
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("Blue: ");
lcd.print (map(bluePower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (14,1);
lcd.print ("->");
}
void redscreen() {
lcd.clear();
lcd.setCursor (0,0);
lcd.print ("Red: ");
lcd.print (map(redPower,0,255,0,100));
lcd.print ("%");
lcd.setCursor (14,1);
lcd.print ("->");
}
void savescreen () {
lcd.clear ();
lcd.setCursor (0,0);
lcd.print ("'SELECT' TO SAVE");
lcd.setCursor (14,1);
lcd.print ("->");
}