I'm pretty new at Arduino, and I'm trying to make my own LCD menu (displayed on a 16x2). I've written some code that will move a custom cursor up and down with the press of either an "up" button or a "down" button. As of right now, I only have it set up for 3 menu items. Everything works fine for the top 2 items, but when I move to the 3rd menu item, the LCD only displays the 3rd item, even though the 2nd should still be visible above it. Once I move the cursor up again, the display returns to normal.
If someone could take a look at my code and help me figure out the problem, I'd really appreciate it.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
boolean bottom = false;
const int upPin = 6;
const int downPin = 7;
unsigned long prevTime = 0;
int upPinState = 0;
int downPinState = 0;
int prevUpPinState = 0;
int prevDownPinState = 2;
String menu1 = "Menu Item 1";
String menu2 = "Menu Item 2";
String menu3 = "Menu Item 3";
String menu4 = "Menu Item 4";
int cursorPosition = 1;
byte myCursor[8] = {
B10000,
B01000,
B00100,
B00010,
B00010,
B00100,
B01000,
B10000,
};
void setup() {
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
lcd.createChar(1, myCursor);
lcd.begin(16, 2);
lcd.setCursor(3,0);
lcd.print("Created By:");
lcd.setCursor(7,1);
lcd.print("JZ");
delay(3000);
lcd.clear();
lcd.print("\x01" + menu1);
lcd.setCursor(1, 1);
lcd.print(menu2);
delay(1000);
Serial.begin(9600);
Serial.println(cursorPosition);
}
void loop() {
int signal = 0;
upPinState = digitalRead(upPin);
downPinState = digitalRead(downPin);
if(upPinState != prevUpPinState && upPinState == HIGH) {
signal = 1;
}
if(downPinState != prevDownPinState && downPinState == HIGH) {
signal = 2;
}
if(cursorPosition == 1){
switch(signal) {
case 1: // up
lcd.clear();
lcd.setCursor(0,0);
lcd.print("\x01" + menu1);
lcd.setCursor(1, 1);
lcd.print(menu2);
cursorPosition = 1;
Serial.println(cursorPosition);
break;
case 2: // down
lcd.clear();
lcd.setCursor(1,0);
lcd.print(menu1);
lcd.setCursor(0,1);
lcd.print("\x01" + menu2);
cursorPosition = 2;
Serial.println(cursorPosition);
break;
}
}
else if(cursorPosition == 2){
switch(signal) {
case 1: // up
lcd.clear();
lcd.setCursor(0,0);
lcd.print("\x01" + menu1);
lcd.setCursor(1, 1);
lcd.print(menu2);
cursorPosition = 1;
Serial.println(cursorPosition);
break;
case 2: // down
lcd.clear();
lcd.setCursor(1,1);
lcd.print(menu2);
lcd.setCursor(0,1);
lcd.print("\x01" + menu3);
cursorPosition = 3;
Serial.println(cursorPosition);
break;
}
}
else if(cursorPosition == 3){
switch(signal) {
case 1: //up
lcd.clear();
lcd.setCursor(0,0);
lcd.print("\x01" + menu2);
lcd.setCursor(1, 1);
lcd.print(menu3);
cursorPosition--;
Serial.println(cursorPosition);
break;
}
}
prevUpPinState = upPinState;
prevDownPinState = downPinState;
}