CCCPaste

"Button Navigation" rev_04

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Button Navigation"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-07 03:59:52

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd display i2c  20x4  button pin11 UPLOW menu up */
	/* button pin10 UP LOW menu enter  button pin12UP LOW */
	/* menu down  menu  Temperatur->MaxTemp->code */
	/* ->MinTeamp->code  Durchfluss(Flow)->MaxFlow->code */
	/* ->MinTeamp->code */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   // https://github.com/evert-arias/EasyButton
#include   // https://github.com/marcoschwartz/LiquidCrystal_I2C

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onUpButtonPressed();
void onDownButtonPressed();
void onEnterButtonPressed();
void displayMenu();
void displaySubMenu(const char* title, const char* code);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t UP_BUTTON_PIN = 11;
const uint8_t DOWN_BUTTON_PIN = 12;
const uint8_t ENTER_BUTTON_PIN = 10;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton upButton(UP_BUTTON_PIN);
EasyButton downButton(UP_BUTTON_PIN);
EasyButton enterButton(UP_BUTTON_PIN);

// Initialize the LCD object with the I2C address (0x27), number of columns (20), and number of rows (4)
LiquidCrystal_I2C lcd(0x27, 20, 4);

// Menu variables
int menuIndex = 0;
const char* menuItems[] = {"Temperatur", "Durchfluss(Flow)"};
const char* subMenuItems[][2] = {{"MaxTemp", "MinTemp"}, {"MaxFlow", "MinFlow"}};
const char* subMenuCodes[][2] = {{"code1", "code2"}, {"code3", "code4"}};
int subMenuIndex = 0;
bool inSubMenu = false;

void setup(void) {
  // Initialize Serial for debugging purposes
  Serial.begin(115200);
  Serial.println();
  Serial.println(">>> EasyButton multiple buttons example <<<");

  // Initialize the buttons
  upButton.begin();
  downButton.begin();
  enterButton.begin();

  // Add the callback functions to be called when the buttons are pressed
  upButton.onPressed(onUpButtonPressed);
  downButton.onPressed(onDownButtonPressed);
  enterButton.onPressed(onEnterButtonPressed);

  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  displayMenu();
}

void loop(void) {
  // Continuously read the status of the buttons
  upButton.read();
  downButton.read();
  enterButton.read();
}

// Callback function to be called when the UP button is pressed
void onUpButtonPressed() {
  if (inSubMenu) {
    subMenuIndex = (subMenuIndex - 1 + 2) % 2; // Move up in the sub-menu
    displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  } else {
    menuIndex = (menuIndex - 1 + 2) % 2; // Move up in the main menu
    displayMenu();
  }
  Serial.println("Up button pressed");
}

// Callback function to be called when the DOWN button is pressed
void onDownButtonPressed() {
  if (inSubMenu) {
    subMenuIndex = (subMenuIndex + 1) % 2; // Move down in the sub-menu
    displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  } else {
    menuIndex = (menuIndex + 1) % 2; // Move down in the main menu
    displayMenu();
  }
  Serial.println("Down button pressed");
}

// Callback function to be called when the ENTER button is pressed
void onEnterButtonPressed() {
  if (inSubMenu) {
    // Perform action based on the selected sub-menu item
    Serial.print("Selected: ");
    Serial.println(subMenuCodes[menuIndex][subMenuIndex]);
  } else {
    inSubMenu = true;
    subMenuIndex = 0;
    displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  }
  Serial.println("Enter button pressed");
}

// Function to display the main menu
void displayMenu() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Menu:");
  lcd.setCursor(0, 1);
  lcd.print(menuItems[menuIndex]);
}

// Function to display the sub-menu
void displaySubMenu(const char* title, const char* code) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(title);
  lcd.setCursor(0, 1);
  lcd.print(subMenuItems[menuIndex][subMenuIndex]);
  lcd.setCursor(0, 2);
  lcd.print(code);
}

/* END CODE */