// LCD1602 to Arduino Uno connection example
#include <LiquidCrystal.h>
unsigned long ST,ET;
//Menu variables
String MainMenuItemPrompt[10]; //this is what is scrolled through when in menu
String SubMenuItemPrompt[10]; //this is what is diplayed on Line 1 on LCD when menu item selected
int SubMenuPossibleResponses[10]; //this is the number of items in the list of possible responses
String SubMenuResponseAlias[10][10]; //this is the value that is shown on line 2 as possible responses are displayed
int SubMenuResponseValue[10][10]; //this stores the value corresponding to the array index selected as an alias
boolean InMenu = false;
int MainMenuIndex=0; //stored the current index in main menu
int SubMenuResponseIndex = 0;
int MenuItems = 8; //index 0 = 1
String LCDLine1;
String LCDLine2;
//change these pin definitions for your schematic
int LeftUpPin = 4;
int LeftDownPin = 5;
int RightUpPin = 2;
int RightDownPin = 3;
int EnterPin = 6;
//int MenuPin = 6; //Future
//Switch State Variable
int SwitchState = 0;
//0 = Nothing Pressed
//1 = Enter Pressed
//2 = Enter Held
//3 = Left Up Pressed
//4 = Left Up Held
//5 = Left Down Pressed
//6 = Left Down Held
//7 = Right Up Pressed
//8 = Right Up Held
//9 = Right Down Pressed
//10 = Right Down Held
//11 = Menu Pressed (Future)
//12 = Menu Held (Future)
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void LoadMenuItems(){
//modify this routine for your menu
MenuItems = 8; //this should be 1 more than the max array numer as array starts at 0
MainMenuItemPrompt[0]=F("Exit Menu");
MainMenuItemPrompt[1]=F("Switch Sides");
MainMenuItemPrompt[2]=F("Start Next Game");
MainMenuItemPrompt[3]=F("Reset Scores");
MainMenuItemPrompt[4]=F("Set Game #");
MainMenuItemPrompt[5]=F("Reset All");
MainMenuItemPrompt[6]=F("Set Team Colors");
MainMenuItemPrompt[7]=F("Set Brightness");
SubMenuItemPrompt[0]=F("");
SubMenuItemPrompt[1]=F("");
SubMenuItemPrompt[2]=F("");
SubMenuItemPrompt[3]=F("");
SubMenuItemPrompt[4]=F(" Set Game #:");
SubMenuItemPrompt[5]=F("");
SubMenuItemPrompt[6]=F("Team A Team B");
SubMenuItemPrompt[7]=F(" Set Brightness");
//this stores the number of acceptable responses stored in array, non-zero indicates data input
//the submenu response array stores acceptable answers
SubMenuPossibleResponses[0] = 0;
SubMenuPossibleResponses[1] = 0;
SubMenuPossibleResponses[2] = 0;
SubMenuPossibleResponses[3] = 0;
SubMenuPossibleResponses[4] = 9;
SubMenuPossibleResponses[5] = 0;
SubMenuPossibleResponses[6] = 0;
SubMenuPossibleResponses[7] = 10;
//set up submenu response arrays only for menu items that require data input
//data will be input based on the array index
SubMenuResponseAlias[4][0] = "1";
SubMenuResponseAlias[4][1] = "2";
SubMenuResponseAlias[4][2] = "3";
SubMenuResponseAlias[4][3] = "4";
SubMenuResponseAlias[4][4] = "5";
SubMenuResponseAlias[4][5] = "6";
SubMenuResponseAlias[4][6] = "7";
SubMenuResponseAlias[4][7] = "8";
SubMenuResponseAlias[4][8] = "9";
SubMenuResponseAlias[6][0] = "Red";
SubMenuResponseAlias[6][1] = "Green";
SubMenuResponseAlias[6][2] = "Blue";
SubMenuResponseAlias[6][3] = "Yellow";
SubMenuResponseAlias[6][4] = "Light Blue";
SubMenuResponseAlias[6][5] = "Purple";
SubMenuResponseAlias[6][6] = "Magenta";
SubMenuResponseAlias[6][7] = "Lime Green";
SubMenuResponseAlias[6][8] = "90%";
SubMenuResponseAlias[6][9] = "Maximum";
SubMenuResponseAlias[7][0] = "Minimum";
SubMenuResponseAlias[7][1] = "20%";
SubMenuResponseAlias[7][2] = "30%";
SubMenuResponseAlias[7][3] = "40%";
SubMenuResponseAlias[7][4] = "50%";
SubMenuResponseAlias[7][5] = "60%";
SubMenuResponseAlias[7][6] = "70%";
SubMenuResponseAlias[7][7] = "80%";
SubMenuResponseAlias[7][8] = "90%";
SubMenuResponseAlias[7][9] = "Maximum";
SubMenuResponseValue[4][0] = 1;
SubMenuResponseValue[4][1] = 2;
SubMenuResponseValue[4][2] = 3;
SubMenuResponseValue[4][3] = 4;
SubMenuResponseValue[4][4] = 5;
SubMenuResponseValue[4][5] = 6;
SubMenuResponseValue[4][6] = 7;
SubMenuResponseValue[4][7] = 8;
SubMenuResponseValue[4][8] = 9;
}
void setup() {
lcd.begin(16, 2);
Serial.begin(115200);
pinMode(LeftUpPin, INPUT_PULLUP);
pinMode(LeftDownPin, INPUT_PULLUP);
pinMode(RightUpPin, INPUT_PULLUP);
pinMode(RightDownPin, INPUT_PULLUP);
pinMode(EnterPin, INPUT_PULLUP);
lcd.print("Menu Test");
lcd.setCursor(0,1);
lcd.print("by Darin");
LoadMenuItems();
}
void loop() {
CheckSwitches();
if (InMenu == true && SwitchState > 0) { //menu item selected
ProcessMenuClick();
}
else if (InMenu == false && SwitchState > 0) { //
ProcessSwitches();
}
}
void ProcessMenuClick(){
}
void ShowMenu() {
MainMenuIndex=3;
InMenu = true;
lcd.clear();
LCDLine1 = MainMenuItemPrompt[MainMenuIndex];
LCDLine2="";
if (MainMenuIndex < (MenuItems - 1)) {
Serial.println("test");
LCDLine2 = MainMenuItemPrompt[MainMenuIndex + 1];
}
Serial.print("LCDLine2:");
Serial.println(LCDLine2);
lcd.print(">");
lcd.print(LCDLine1);
lcd.setCursor(1,1);
lcd.print(LCDLine2);
}
void ProcessSwitches(){
switch (SwitchState) {
case 0: // Exit, no button press
break;
case 1: //Enter Pressed Enter Menu
Serial.println("Enter Press...Enter Menu");
ShowMenu();
InMenu=true;
break;
case 2: //Enter Held
Serial.println("Enter Held");
break;
case 3: //Left Up Pressed
Serial.println("Left Up Press");
break;
case 4: //Left Up Held
Serial.println("Left Up Held");
break;
case 5: //Left Down Pressed
Serial.println("Left Down Press");
break;
case 6: //Left Down Held
Serial.println("Left Down Held");
break;
case 7: //Right Up Pressed
Serial.println("Right Up Press");
break;
case 8: //Right Up Held
Serial.println("Right Up Held");
break;
case 9: //Right Down Pressed
Serial.println("Right Down Press");
break;
case 10: //Right Down Held
Serial.println("Right Down Held");
break;
}
SwitchState = 0; //reset
delay(500); //to eliminate bounce back to press
}
void CheckSwitches(){
//0 = Nothing Pressed
//1 = Enter Pressed
//2 = Enter Held
//3 = Left Up Pressed
//4 = Left Up Held
//5 = Left Down Pressed
//6 = Left Down Held
//7 = Right Up Pressed
//8 = Right Up Held
//9 = Right Down Pressed
//10 = Right Down Held
//11 = Menu Pressed (Future)
//12 = Menu Held (Future)
SwitchState = 0; //reset
//check Enter Switch first
if (digitalRead(EnterPin) == LOW){
ST = millis();
while (digitalRead(EnterPin) == LOW){ //debounce
delay(1);
ET = millis()-ST;
}
//if we got here we have a debounced button press
if (ET > 50 && ET <750){ //short press
SwitchState = 1;
return;
}
else if (ET > 750 ){ //long press
SwitchState = 2;
return;
}
}
//check Left Up
if (digitalRead(LeftUpPin) == LOW){
ST = millis();
while (digitalRead(LeftUpPin) == LOW){ //debounce
delay(1);
ET = millis()-ST;
}
//if we got here we have a debounced button press
if (ET > 50 && ET <750){ //short press
SwitchState = 3;
return;
}
else if (ET > 750 ){ //long press
SwitchState = 4;
return;
}
}
//check Left Down
if (digitalRead(LeftDownPin) == LOW){
ST = millis();
while (digitalRead(LeftDownPin) == LOW){ //debounce
delay(1);
ET = millis()-ST;
}
//if we got here we have a debounced button press
if (ET > 50 && ET <750){ //short press
SwitchState = 5;
return;
}
else if (ET > 750 ){ //long press
SwitchState = 6;
return;
}
}
//check Right Up
if (digitalRead(RightUpPin) == LOW){
ST = millis();
while (digitalRead(RightUpPin) == LOW){ //debounce
delay(1);
ET = millis()-ST;
}
//if we got here we have a debounced button press
if (ET > 50 && ET <750){ //short press
SwitchState = 7;
return;
}
else if (ET > 750 ){ //long press
SwitchState = 8;
delay(500); //to eliminate bounce back to press
return;
}
}
//check Right Down
if (digitalRead(RightDownPin) == LOW){
ST = millis();
while (digitalRead(RightDownPin) == LOW){ //debounce
delay(1);
ET = millis()-ST;
}
//if we got here we have a debounced button press
if (ET > 50 && ET <750){ //short press
SwitchState = 9;
return;
}
else if (ET > 750 ){ //long press
SwitchState = 10;
return;
}
}
} //end of check switches