As I promised, I'm publishing my LCD screen menu code for Arduino.
#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// Main menu array
String mainMenu[10];
// Main menu default choice
int mainSwitch = 0;
// Amount of items in our main menu.
// Can be found by formula:
// (Amount_of_items - 1)
#define mainMenuItems 2
// Water menu array
String waterMenu[10];
// Water menu default choice
int waterSwitch = 0;
#define waterMenuItems 1
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("Main menu: "); // print a simple message
// Filling our main menu items
mainMenu[0] = "Light ";
mainMenu[1] = "Water ";
mainMenu[2] = "Stop ";
// Filling our water menu items
waterMenu[0] = "item1 ";
waterMenu[1] = "item2 ";
lcd.setCursor(0,1);
lcd.print(mainMenu[0]);
}
void cleanScreen()
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
void light()
{
lcd.setCursor(0,0);
lcd.print(mainMenu[0]); // print a simple message
lcd.setCursor(0,1);
// Loop for reading buttons
while(true)
{
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
break;
}
// back to main menu on the left button press
case btnLEFT:
{
goto exit;
break;
}
}
}
exit:
lcd.setCursor(0,0);
lcd.print("Main menu: ");
lcd.setCursor(0,1);
lcd.print(mainMenu[0]);
return;
}
void water()
{
lcd.setCursor(0,0);
lcd.print(mainMenu[1]);
lcd.setCursor(0,1);
while(true)
{
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
break;
}
// back to main menu on the left button press
case btnLEFT:
{
goto exit;
break;
}
}
}
exit:
lcd.setCursor(0,0);
lcd.print("Main menu: "); // print a simple message
lcd.setCursor(0,1);
lcd.print(mainMenu[1]);
return;
}
void aStop()
{
lcd.setCursor(0,0);
lcd.print(mainMenu[2]);
lcd.setCursor(0,1);
while(true)
{
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
break;
}
// back to main menu on the left button press
case btnLEFT:
{
goto exit;
break;
}
}
}
exit:
lcd.setCursor(0,0);
lcd.print("Main menu: ");
lcd.setCursor(0,1);
lcd.print(mainMenu[2]);
return;
}
// Main loop
void loop()
{
// move to the begining of the second line
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons(); // read the buttons
// depending on which button was pushed,
// we perform an action
switch (lcd_key)
{
// Enter a sub menu on Right button press
case btnRIGHT:
{
cleanScreen();
if(mainSwitch == 0)
{
light();
}
else if(mainSwitch == 1)
{
water();
}
else if(mainSwitch == 2)
{
aStop();
}
break;
}
// Move to the next sub menu (UP)
case btnUP:
{
if(mainSwitch == 0)
{
mainSwitch = mainMenuItems;
}
else
{
mainSwitch = mainSwitch - 1;
}
lcd.print(mainMenu[mainSwitch]);
delay(300);
break;
}
// Move to the next sub menu (DOWN)
case btnDOWN:
{
if(mainSwitch > mainMenuItems - 1)
{
mainSwitch = 0;
}
else
{
mainSwitch = mainSwitch + 1;
}
lcd.print(mainMenu[mainSwitch]);
delay(300);
break;
}
}
}
Thank you
ReplyDeleteBest
ReplyDeletei tried this menu on my arduino pro micro and it just constantly cycles through Water, Stop, Light...
ReplyDelete