I am new with Arduino and I am working on a project.
I am using a LCD screen and I want that when a key on the 4x4 keypad is pressed it will move to main menu screen and then the user will have to choose an action.
Now the problem is that the second "if" is not working and I think there is a problem with the "getkey()" (it moves to the main menu and then when I press 'A' it doesn't go to the "Feeding" screen).
You realize, I hope, that getKey() does NOT wait for a key to be pressed. It is highly unlikely that you can press a key fast enough for all three getKey() calls to read different key presses.
PaulS:
You realize, I hope, that getKey() does NOT wait for a key to be pressed. It is highly unlikely that you can press a key fast enough for all three getKey() calls to read different key presses.
This code didn't work like I wanted it to work, when I pressed 'A' it immediately showed me the "Feeding" screen. I also understand why. when I press 'A' these two conditionals are correct so if I'll put a delay in the first one it will show me the main menu and then the "Feeding" screen.
So, what should I do to make it work like I want it to?
So the first key does nothing but 'enable' the 'main screen' (which shows two options) and when the 'main screen' is enabled the 'A' key will pick the option for the "Feeding..." page.
void loop() {
static int screen = 0;
char key = keypad.getKey();
if (key) {
switch (screen) {
case 0: // Beginning Screen
screen = 1; // Switch to Options Menu Screen
lcd.clear();
lcd.print("Feed>A");
lcd.setCursor(0, 1);
lcd.print("Set Feed Times>B");
break;
case 1: // Options Manu Screen
if (key == 'A') {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Feeding...");
//motor action
delay(4000);
screen_finish();
screen = 0; // Go back to Beginning Screen
}
if (key == 'B') {
screen = 2; // Go to Set Feeding Times Screen
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Set Feeding Times");
}
break;
case 2: // Set Feeding Times Screen
break;
}
}
}
johnwasser:
So the first key does nothing but 'enable' the 'main screen' (which shows two options) and when the 'main screen' is enabled the 'A' key will pick the option for the "Feeding..." page.
void loop() {
static int screen = 0;
char key = keypad.getKey();
if (key) {
switch (screen) {
case 0: // Beginning Screen
screen = 1; // Switch to Options Menu Screen
lcd.clear();
lcd.print("Feed>A");
lcd.setCursor(0, 1);
lcd.print("Set Feed Times>B");
break;
case 1: // Options Manu Screen
if (key == 'A') {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Feeding...");
//motor action
delay(4000);
screen_finish();
screen = 0; // Go back to Beginning Screen
}
if (key == 'B') {
screen = 2; // Go to Set Feeding Times Screen
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Set Feeding Times");
}
break;
case 2: // Set Feeding Times Screen
break;
}
}
}
Wow! thank you very much, you helped me a lot! didn't know of the "switch" command.