How to escape while control structure with implement of switch case

holmes4:

  if(key)  // Check for a valid key.

{
   switch (key)




does not check that "key" is valid!. It say if key equals the numeric value of the boolean TRUE enter the "then" part of the if statement!

replace if(key){ with something that performs a range check on the value of key eg

if ((key >'0' AND key<='9') OR key = '#' or key = '*'){

Mark

i think the outcome is still the same if i use if control structure. below is the programming code. still yet, display once the number respective to the keypad number. not repeatedly the number respective to the keypad number that being pressed.

/*  Keypadtest.pde
 *
 *  Demonstrate the simplest use of the  keypad library.
 *
 *  The first step is to connect your keypad to the
 *  Arduino  using the pin numbers listed below in
 *  rowPins[] and colPins[]. If you want to use different
 *  pins then  you  can  change  the  numbers below to
 *  match your setup.
 *
 */
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns

// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 0, 2, 9, 10 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 11, 12, 13 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  // set up SERIAL MONITOR: 
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
    
  if (key == '1') {
  Serial.println("1");
  }

  else if (key == '2') {
  Serial.println("2");
  }
  
  else if (key == '3') {
  Serial.println("3");
  }
  
  else if (key == '4') {
  Serial.println("4");
  }
  
  else if (key == '5') {
  Serial.println("5");
  }
  
  else if (key == '6') {
  Serial.println("6");
  }
  
  else if (key == '7') {
  Serial.println("7");
  }
  
  else if (key == '8') {
  Serial.println("8");
  }
  
  else if (key == '9') {
  Serial.println("9");
  }
  
  else if (key == '*') {
  Serial.println("*");
  }

  else if (key == '0') {
  Serial.println("0");
  }
  
  else if (key == '#') {
  Serial.println("#");
  }
}