#define upButton 2
#define downButton 3
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
const int ArrayLength = 15;
String menuItems[ArrayLength] = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12", "Item 13", "Item 14", "Item 15"};
int currIndex = 0;
int currDisolayRow = 0;
bool currsorMode = 0;
void setup() {
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome....");
delay(1000);
lcd.clear();
clearArrow();
lcd.setCursor(0, 0);
lcd.print(">");
drawMenu(0);
Serial.begin(115200);
Serial.println(currIndex);
}
void clearArrow() {
// column,row
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
void drawMenu(int startItem) {
for (int i = 0; i < 4; i++) {
lcd.setCursor(1, i);
lcd.print(menuItems[i + startItem]);
if (currIndex == ArrayLength - 1 and currsorMode == 1) {
currIndex = 0;
currDisolayRow = 0;
}
}
}
void loop() {
bool upState = digitalRead(upButton) ;
bool downState = digitalRead(downButton);
if (downState == LOW and currIndex < ArrayLength - 1) {
currsorMode = 0;
if ( currDisolayRow > 2 ) {
currIndex ++;
//currDisolayRow++;
lcd.clear();
lcd.setCursor( 0, currDisolayRow);
lcd.print(">");
drawMenu(currIndex - 3);
Serial.println(currIndex);
delay(250);
} else {
currIndex ++;
currDisolayRow++;
clearArrow();
lcd.setCursor( 0, currDisolayRow);
lcd.print(">");
//Serial.println(currIndex);
delay(250);
}
}
if (upState == LOW ) {
Serial.println(currIndex);
currsorMode = 1;
if ( currDisolayRow < 0 ) {
Serial.println("Display Last Item");
currIndex = ArrayLength - 1;
lcd.clear();
lcd.setCursor( 0, currDisolayRow);
lcd.print(">");
drawMenu(ArrayLength - 1);
//Serial.println(currIndex);
delay(250);
} else {
currIndex --;
currDisolayRow--;
clearArrow();
lcd.setCursor( 0, currDisolayRow);
lcd.print(">");
//Serial.println(currIndex);
delay(250);
}
}
}