Keypad code problem

Hello,

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.

this is the code for it:

  void loop()
  {
    char key1 = keypad.getKey();
    if(key1) //main menu
    {
      key1 =! keypad.getKey();
      char key2 = keypad.getKey();
      lcd.clear();
      lcd.print("Feed>A");
      lcd.setCursor(0,1);
      lcd.print("Set Feed Times>B");
      if(key2=='A')
      {
        lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("Feeding...");
        //motor action
        delay(4000);
        screen_finish();
      }
    }
  }

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).

Thanks for the help.

      key1 =! keypad.getKey();

What do you think this is doing?

      char key2 = keypad.getKey();
      lcd.clear();
      lcd.print("Feed>A");
      lcd.setCursor(0,1);
      lcd.print("Set Feed Times>B");
      if(key2=='A')

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:       key1 =! keypad.getKey();

Oops. I actually forgot it is a conditional. thx.

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.

At first I tried this code:

 void loop()
  {
    char key = keypad.getKey();
    int screen=0;
    if(key)
    {
      screen=1;
      lcd.clear();
      lcd.print("Feed>A");
      lcd.setCursor(0,1);
      lcd.print("Set Feed Times>B");
      if(screen==1&&key=='A')
      {
        lcd.clear();
        lcd.setCursor(2,0);
        lcd.print("Feeding...");
        //motor action
        delay(4000);
        screen_finish();
      }
    }
  }

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.

The "switch" statement is good when you have a bunch of code that looks like:

 int integer;
if (integer == 1) {
//do something
}
if (integer == 27) {
// do something
}
if (integer == 53) {
// do something
}
.
.
.

It's a shorthand version of this common pattern, just like a "for (initial; test; increment)" loop is shorthand for:

{
  initial;
  while (test) {
     // do something
     increment;
  }
}