#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address and dimensions of the LCD
// Define menu states
enum MenuState {
MAIN_MENU,
SUBMENU_1,
SUBMENU_2,
SUBMENU_3,
FLOAT_VARIABLES,
LONG_CONSTANTS,
ULONG_VARIABLES,
INT_CONSTANTS
};
// Define button pins
const int LEFT_BUTTON_PIN = 2;
const int RIGHT_BUTTON_PIN = 3;
const int UP_BUTTON_PIN = 4;
const int DOWN_BUTTON_PIN = 5;
// Define variables
float SourceLowV = 10.70;
float SourceOverV = 14.30;
float BatLowV = 10.70;
float BatOverV = 14.30;
long Long1 = 1234567890;
long Long2 = -987654321;
unsigned long ulongVar1 = 4294967295; // Maximum value of unsigned long
unsigned long ulongVar2 = 987654321;
const int constInt1 = 100;
const int constInt2 = -50;
// Define initial menu state
MenuState currentMenu = MAIN_MENU;
// Function prototypes
void displayMenu();
void displaySubmenu1();
void displaySubmenu2();
void displaySubmenu3();
void updateMenu();
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
pinMode(LEFT_BUTTON_PIN,INPUT_PULLUP);
pinMode(RIGHT_BUTTON_PIN,INPUT_PULLUP);
pinMode(UP_BUTTON_PIN,INPUT_PULLUP);
pinMode(DOWN_BUTTON_PIN,INPUT_PULLUP);
// Print initial message on LCD
lcd.print("Charger Control");
lcd.setCursor(0, 1);
lcd.print("V.1.0.");
delay(3000);
}
void loop() {
// Display current menu
displayMenu();
// Handle button press
int buttonPressed = checkButtons();
// Change menu or adjust variables based on button press
switch (buttonPressed) {
case 1:
navigateMenu(-1); // Button 1 (left or previous)
break;
case 2:
navigateMenu(1); // Button 2 (right or next)
break;
case 3:
adjustVariable(1); // Button 3 (up or increase)
break;
case 4:
adjustVariable(-1); // Button 4 (down or decrease)
break;
case 5:
// Additional functionality for button 5 (select) if needed
break;
default:
break;
}
}
// Function to navigate through menu based on button press
void navigateMenu(int button) {
switch (currentMenu) {
case MAIN_MENU:
if (button == 1) {
currentMenu = FLOAT_VARIABLES; // Change to float menu
Serial.println("Left button pressed");
}
else if (button == 2) {
currentMenu = LONG_CONSTANTS; // Change to long menu
Serial.println("RIGHT button pressed");
} else if (button == 3) {
currentMenu = ULONG_VARIABLES; // Change to unsigned long menu
Serial.println("UP button pressed");
} else if (button == 4) {
currentMenu = INT_CONSTANTS; // Change to int menu
Serial.println("DOWN button pressed");
}
break;
case FLOAT_VARIABLES:
case LONG_CONSTANTS:
case ULONG_VARIABLES:
case INT_CONSTANTS:
if (button == 5) {
currentMenu = MAIN_MENU; // Return to main menu
}
break;
}
}
// Function to adjust variable values based on button press
void adjustVariable(int button) {
switch (currentMenu) {
case FLOAT_VARIABLES:
if (button == 1) {
//Serial.println("Left button pressed");
SourceLowV += 0.1; // Increase float variable by 0.1
}else if (button == 2) {
// Serial.println("Right button pressed");
SourceLowV -= 0.1; // Decrease float variable by 0.1
}else if (button == 3) {
// Serial.println("Up button pressed");
SourceOverV += 0.1; // Increase float variable by 0.1
}else if (button == 4) {
// Serial.println("Down button pressed");
SourceOverV -= 0.1; // Decrease float variable by 0.1
}
break;
case LONG_CONSTANTS:
if (button == 1) {
Long1 += 1; // Increase const long variable by 100
} else if (button == 2) {
Long1 -= 1; // Decrease const long variable by 100
} else if (button == 3) {
Long2 += 1; // Increase const long variable by 100
} else if (button == 4) {
Long2 -= 1; // Decrease const long variable by 100
}
break;
// Add cases for ULONG_MENU, INT_MENU similarly
}
}
// Function to display current menu and variable values on LCD
void displayMenu() {
//lcd.clear();
switch (currentMenu) {
case MAIN_MENU:
lcd.setCursor(0, 0);
lcd.print("Main Menu ");
// Display options for other menus if needed
break;
case FLOAT_VARIABLES:
lcd.setCursor(0, 0);
lcd.print("SourceLowV:");
lcd.setCursor(11, 0);
lcd.print(SourceLowV);
lcd.setCursor(0, 1);
lcd.print("SourceOveV:");
lcd.setCursor(11, 1);
lcd.print(SourceOverV);
break;
case LONG_CONSTANTS:
lcd.setCursor(0, 0);
lcd.print("ConstLong1: ");
lcd.print(Long1);
lcd.setCursor(0, 1);
lcd.print("ConstLong2: ");
lcd.print(Long2);
break;
// Add cases for other menu states similarly
}
}
void displaySubmenu1() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Submenu 1");
// Add additional submenu 1 options if needed
}
void displaySubmenu2() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Submenu 2");
// Add additional submenu 2 options if needed
}
void displaySubmenu3() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Submenu 3");
// Add additional submenu 3 options if needed
}
void updateMenu(int button) {
switch (currentMenu) {
case MAIN_MENU:
displayMenu();
// Check for button press to navigate to submenu
if (button == 1) {
currentMenu = SUBMENU_1;
} else if (button == 2) {
currentMenu = SUBMENU_2;
} else if (button == 3) {
currentMenu = SUBMENU_3;
}
break;
case SUBMENU_1:
displaySubmenu1();
// Add logic to navigate back to main menu or perform actions
break;
case SUBMENU_2:
displaySubmenu2();
// Add logic to navigate back to main menu or perform actions
break;
case SUBMENU_3:
displaySubmenu3();
// Add logic to navigate back to main menu or perform actions
break;
}
}
// Function to check which button is pressed
int checkButtons() {
// Check which button is pressed and return the pin number of the pressed button
if (digitalRead(LEFT_BUTTON_PIN) == LOW) {
delay(50); // Debounce
return LEFT_BUTTON_PIN;
} else if (digitalRead(RIGHT_BUTTON_PIN) == LOW) {
delay(50); // Debounce
return RIGHT_BUTTON_PIN;
} else if (digitalRead(UP_BUTTON_PIN) == LOW) {
delay(50); // Debounce
return UP_BUTTON_PIN;
} else if (digitalRead(DOWN_BUTTON_PIN) == LOW) {
delay(50); // Debounce
return DOWN_BUTTON_PIN;
}
return 0;
}