#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define nextButton 3
#define selectButton 8
int PresentState = 0; // can't go over 3
int previousState = -1; // to track changes in state
bool isShowTitle = true;
volatile bool nextButtonPressed = false;
unsigned long previousMillis = 0;
void setup()
{
lcd.init();
lcd.backlight();
pinMode(nextButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(nextButton), goNext, FALLING);
}
void loop()
{
if (isShowTitle == true)
{
lcd.setCursor(4, 0);
lcd.print("Select");
lcd.setCursor(3, 1);
lcd.print("an option");
}
else if (isShowTitle == false && (previousState != PresentState))
{
updateLCD();
previousState = PresentState;
}
int selectButtonState = digitalRead(selectButton);
if (selectButtonState == LOW)
{
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Selected");
lcd.setCursor(4, 1);
switch (PresentState)
{
case 1:
lcd.print("Option A");
break;
case 2:
lcd.print("Option B");
break;
case 3:
lcd.print("Option C");
break;
default:
break;
}
// Wait for the select button to be released
while (digitalRead(selectButton) == LOW)
{
}
}
nextButtonPressed = false;
}
void handleNextButton()
{
nextButtonPressed = true;
}
void goNext()
{
if (millis() - previousMillis >= 400){
handleNextButton();
if (nextButtonPressed == true){
PresentState = (PresentState % 3) + 1;
isShowTitle = false;
}
previousMillis = millis();
}
}
void updateLCD()
{
lcd.clear();
lcd.setCursor(2, 0);
switch (PresentState)
{
case 1:
lcd.print("Option A");
break;
case 2:
lcd.print("Option B");
break;
case 3:
lcd.print("Option C");
break;
default:
break;
}
}