#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int upButton = 9;
int downButton = 8;
int selectButton = 10;
int menu = 1;
int menuL2i = 0;
int menuL2 = 1;
int Action = 0;
String ML1[6]={"Menu 1","Menu 2","Menu 3","Menu 4","Menu 5","Menu 6"};
const char* ML2[6][4]={
{"SubMenu 11","SubMenu 12","SubMenu 13","SubMenu 14"},
{"SubMenu 21","SubMenu 22","SubMenu 23","SubMenu 24"},
{"SubMenu 31","SubMenu 32","SubMenu 33","SubMenu 34"},
{"SubMenu 41","SubMenu 42","SubMenu 43","SubMenu 44"},
{"SubMenu 51","SubMenu 52","SubMenu 53","SubMenu 54"},
{"SubMenu 61","SubMenu 62","SubMenu 63","SubMenu 64"}};
void setup() {
// lcd.init();
lcd.begin(16,2);
lcd.backlight();
pinMode(upButton, INPUT_PULLUP); // input pullup for arduino with internal resestor High means off or Open and Low means on or Closed
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
intro();
updateMenu();
}
void loop() {
if (!digitalRead(downButton) && menu < 6 && menuL2i==0){ // if downbutton not true used for booleans -------- menuL2i is indecator to be inside Level 2 menu
menu++;
updateMenu();
delay(150);
while (!digitalRead(downButton));
}
if (!digitalRead(upButton) && menu > 1 && menuL2i==0){ // menu should start at 1
menu--;
updateMenu();
delay(150);
while(!digitalRead(upButton));
}
if (!digitalRead(selectButton) && menuL2i==0){
menuL2i=1;
updateMenuL2();
delay(150);
while (!digitalRead(selectButton));
}
//-----------------------------------------------------------------
if (!digitalRead(downButton) && menuL2 < 5 && menuL2i==1){ // if downbutton not true used for booleans
menuL2++;
updateMenuL2();
delay(150);
while (!digitalRead(downButton));
}
if (!digitalRead(upButton) && menuL2 > 1 && menuL2i==1){ // menu should start at 1
menuL2--;
updateMenuL2();
delay(150);
while(!digitalRead(upButton));
}
if (!digitalRead(selectButton) && menuL2i==1){
Action = (menu*10)+menuL2;
executeAction();
Action = 0;
if(menuL2i==1) updateMenuL2();
delay(150);
while (!digitalRead(selectButton));
}
}
//---------------------------------------------------------------
void updateMenu() {
switch (menu) {
case 1:
lcd.clear();
lcd.print(">");
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(2, 1);
lcd.print(ML1[(menu)]);
break;
case 2:
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-2)]);
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(2, 1);
lcd.print(ML1[(menu-1)]);
break;
case 3:
lcd.clear();
lcd.print(">");
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(2, 1);
lcd.print(ML1[(menu)]);
break;
case 4:
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-2)]);
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(2, 1);
lcd.print(ML1[(menu-1)]);
break;
case 5:
lcd.clear();
lcd.print(">");
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(2, 1);
lcd.print(ML1[(menu)]);
break;
case 6:
lcd.clear();
lcd.setCursor(2, 0);
lcd.print(ML1[(menu-2)]);
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(2, 1);
lcd.print(ML1[(menu-1)]);
break;
}
}
void updateMenuL2() {
switch (menuL2) {
case 1:
lcd.clear();
lcd.print("**");
lcd.setCursor(3, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(14, 0);
lcd.print("**");
lcd.setCursor(2, 1);
lcd.print(ML2[(menu-1)][(menuL2-1)]);
break;
case 2:
lcd.clear();
lcd.print("**");
lcd.setCursor(3, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(14, 0);
lcd.print("**");
lcd.setCursor(2, 1);
lcd.print(ML2[(menu-1)][(menuL2-1)]);
break;
case 3:
lcd.clear();
lcd.print("**");
lcd.setCursor(3, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(14, 0);
lcd.print("**");
lcd.setCursor(2, 1);
lcd.print(ML2[(menu-1)][(menuL2-1)]);
break;
case 4:
lcd.clear();
lcd.print("**");
lcd.setCursor(3, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(14, 0);
lcd.print("**");
lcd.setCursor(2, 1);
lcd.print(ML2[(menu-1)][(menuL2-1)]);
break;
case 5:
lcd.clear();
lcd.print("**");
lcd.setCursor(3, 0);
lcd.print(ML1[(menu-1)]);
lcd.setCursor(14, 0);
lcd.print("**");
lcd.setCursor(2, 1);
lcd.print(" >>BACK<<");
break;
}
}
void executeAction() {
switch (Action) {
case 11:
action11();
break;
case 12:
action12();
break;
case 13:
action13();
break;
case 14:
action14();
break;
case 15:
back();
break;
case 21:
action21();
break;
case 22:
action22();
break;
case 23:
action23();
break;
case 24:
action24();
break;
case 25:
back();
break;
case 31:
action31();
break;
case 32:
action32();
break;
case 33:
action33();
break;
case 34:
action34();
break;
case 35:
back();
break;
case 41:
action41();
break;
case 42:
action42();
break;
case 43:
action43();
break;
case 44:
action44();
break;
case 45:
back();
break;
case 51:
action51();
break;
case 52:
action52();
break;
case 53:
action53();
break;
case 54:
action54();
break;
case 55:
back();
break;
case 61:
action61();
break;
case 62:
action62();
break;
case 63:
action63();
break;
case 64:
action64();
break;
case 65:
back();
break;
}
}
void action11() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action12() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action13() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action14() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action21() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action22() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action23() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action24() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action31() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action32() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action33() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action34() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action41() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action42() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action43() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action44() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action51() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action52() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action53() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action54() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void action61() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
// Rename variables to avoid conflicts with Arduino pin names
char signChar = '+'; // Sign (+ or -)
int digit1 = 2, digit2 = 1, digit3 = 4, digit4 = 2, digit5 = 2, digit6 = 4, digit7 = 9, digit8 = 3, digit9 = 4, digit10 = 6, digit11 = 8, digit12 = 5, digit13 = 0;
int currentDigit = 0; // Index of the current digit being edited
// Function to adjust the value
void action62() {
delay(200);
lcd.clear();
displayValue();
while (true) {
// Check for select button press
if (digitalRead(selectButton) == LOW) {
delay(200); // Debounce delay
currentDigit++; // Move to the next digit
if (currentDigit == 14) { // If last digit is reached, exit the menu
lcd.clear();
currentDigit = 0;
lcd.setCursor(0, 0);
lcd.print("Returning...");
delay(500); // Display message before returning to the menu
return; // Exit the function and return to the previous menu
}
displayValue();
}
// Check for up button press
if (digitalRead(upButton) == LOW) {
delay(200); // Debounce delay
incrementDigit(currentDigit);
displayValue();
}
// Check for down button press
if (digitalRead(downButton) == LOW) {
delay(200); // Debounce delay
decrementDigit(currentDigit);
displayValue();
}
delay(100); // Small delay to reduce button bounce
}
}
void displayValue() {
lcd.clear();
// Print the caret marker
int cursorPos = currentDigit < 3 ? currentDigit : currentDigit + 1; // Adjust cursor position for fixed comma
lcd.setCursor(cursorPos, 0);
lcd.write('v'); // Caret marker below the current digit
// Print the value with the current digit highlighted
lcd.setCursor(0, 1);
lcd.print(signChar);
lcd.print(digit1);
lcd.print(digit2);
lcd.print(".");
lcd.print(digit3);
lcd.print(digit4);
lcd.print(digit5);
lcd.print(digit6);
lcd.print(digit7);
lcd.print(digit8);
lcd.print(digit9);
lcd.print(digit10);
lcd.print(digit11);
lcd.print(digit12);
lcd.print(digit13);
}
void incrementDigit(int pos) {
if (pos == 0) {
signChar = (signChar == '+') ? '-' : '+';
} else {
int *current = getDigitPointer(pos);
*current = (*current + 1) % 10;
}
}
void decrementDigit(int pos) {
if (pos == 0) {
signChar = (signChar == '+') ? '-' : '+';
} else {
int *current = getDigitPointer(pos);
*current = (*current - 1 + 10) % 10;
}
}
int* getDigitPointer(int pos) {
switch (pos) {
case 1: return &digit1;
case 2: return &digit2;
case 3: return &digit3;
case 4: return &digit4;
case 5: return &digit5;
case 6: return &digit6;
case 7: return &digit7;
case 8: return &digit8;
case 9: return &digit9;
case 10: return &digit10;
case 11: return &digit11;
case 12: return &digit12;
case 13: return &digit13;
default: return nullptr;
}
}
// double value = 21.4224934685043; // Declare this outside of action62 to retain the value
// double previousValue = 21.4224934685043; // To store the previous value and detect changes
// void action62() {
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Set Value:");
// lcd.setCursor(0, 1);
// lcd.print(value, 13); // Display the current value with 13 decimal places
// delay(300); // Short delay for smoother button handling
// unsigned long pressStartTime; // To track how long the button is pressed
// bool upPressed = false, downPressed = false;
// while (true) {
// if (value != previousValue) {
// lcd.setCursor(0, 1);
// lcd.print(" "); // Clear the previous number (optional)
// lcd.setCursor(0, 1);
// lcd.print(value, 13); // Display the current value with 13 decimal places
// previousValue = value; // Update the previousValue
// }
// // Check for up button press
// if (digitalRead(upButton) == LOW) {
// if (!upPressed) { // Button just pressed
// upPressed = true;
// pressStartTime = millis();
// } else { // Button held down
// unsigned long pressDuration = millis() - pressStartTime;
// double increment = pressDuration / 1000.0; // Acceleration based on time held
// if (value + increment <= 90.0000000000000) { // Ensure the value does not exceed 90
// value += increment;
// } else {
// value = 90.0000000000000;
// }
// }
// delay(50); // Short debounce delay
// } else {
// upPressed = false;
// }
// // Check for down button press
// if (digitalRead(downButton) == LOW) {
// if (!downPressed) { // Button just pressed
// downPressed = true;
// pressStartTime = millis();
// } else { // Button held down
// unsigned long pressDuration = millis() - pressStartTime;
// double decrement = pressDuration / 1000.0; // Acceleration based on time held
// if (value - decrement >= -90.0000000000000) { // Ensure the value does not go below -90
// value -= decrement;
// } else {
// value = -90.0000000000000;
// }
// }
// delay(50); // Short debounce delay
// } else {
// downPressed = false;
// }
// // Check for select button press
// if (digitalRead(selectButton) == LOW) { // Assuming LOW means the button is pressed
// break; // Exit the loop and return to the previous menu
// }
// delay(100);
// }
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Returning...");
// delay(500); // Display message before returning to the menu
// }
// void action62() {
// lcd.clear();
// lcd.print(" !! Ordering !!");
// for (int i=0; i <= Action; i++){
// lcd.setCursor(2, 1);
// lcd.print(i);
// delay(200);
// }
// delay(1500);
// }
// void action63() {
// lcd.clear();
// lcd.print(" !! Ordering !!");
// for (int i=0; i <= Action; i++){
// lcd.setCursor(2, 1);
// lcd.print(i);
// delay(200);
// }
// delay(1500);
// }
int valuen = 2000; // Declare this outside of action63 to retain the value
int previousValuen = 2000; // To store the previous value and detect changes
void action63() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Value:");
lcd.setCursor(4, 1);
lcd.print(valuen);
delay(300); // Short delay for smoother button handling
while (true) {
if (valuen != previousValuen) {
lcd.setCursor(4, 1);
lcd.print(" "); // Clear the previous number (optional)
lcd.setCursor(4, 1);
lcd.print(valuen); // Display the current value
previousValuen = valuen; // Update the previousValue
}
// Check for up button press
if (digitalRead(upButton) == LOW) { // Assuming LOW means the button is pressed
if (valuen < 2025) { // Ensure the value does not exceed 2025
valuen++;
}
delay(300); // Debounce delay
}
// Check for down button press
if (digitalRead(downButton) == LOW) { // Assuming LOW means the button is pressed
if (valuen > 2000) { // Ensure the value does not go below 2000
valuen--;
}
delay(300); // Debounce delay
}
// Check for select button press
if (digitalRead(selectButton) == LOW) { // Assuming LOW means the button is pressed
break; // Exit the loop and return to the previous menu
}
delay(100);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Returning...");
delay(500); // Display message before returning to the menu
}
void action64() {
lcd.clear();
lcd.print(" !! Ordering !!");
for (int i=0; i <= Action; i++){
lcd.setCursor(2, 1);
lcd.print(i);
delay(200);
}
delay(1500);
}
void back() {
lcd.clear();
menuL2i = 0;
menuL2 = 1;
updateMenu();
delay(500);
}
void intro() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LCD menu Project");
for (int b=0; b<3; b++){
lcd.setCursor(0, 1);
lcd.print(" Dr. Afifi");
delay(30);
lcd.setCursor(0, 1);
lcd.print(" Dr. Afifi p");
delay(30);
lcd.setCursor(0, 1);
lcd.print(" Afifi phafif");
delay(30);
lcd.setCursor(0, 1);
lcd.print("i phafifi@gma");
delay(30);
lcd.setCursor(0, 1);
lcd.print(" [email protected]");
delay(30);
lcd.setCursor(0, 1);
lcd.print("[email protected] ");
delay(30);
lcd.setCursor(0, 1);
lcd.print("[email protected] Dr");
delay(30);
}
}