Go Down

Topic: How to escape while control structure with implement of switch case (Read 1 time) previous topic - next topic

CrossRoads

That's  because you keep declaring these as new variable every pass thru loop here, which I think sets new_key to 0 every time - you could add a serial.print and confirm.
Code: [Select]

void loop()
{
  char key = kpd.getKey();
  char new_key;


Move
char key;
char new_key;
into the pre-setup area,
and then just use them like I showed in #8.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

holmes4

Going back to the OP's original code

Code: [Select]

void loop()
{
 char key = kpd.getKey();
 if(key)  // Check for a valid key.
 {
   switch (key)
   {
     case '1':
       while(key = '1'){
       Serial.println("1");
       }
       break;
     case '2':
       Serial.println("2");
       break;
     case '3':
       Serial.println("3");
       break;
     case '4':
       Serial.println("4");
       break;
     case '5':
       Serial.println("5");
       break;
     case '6':
       Serial.println("6");
       break;
     case '7':
       Serial.println("7");
       break;
     case '8':
       Serial.println("8");
       break;
     case '9':
       Serial.println("9");
       break;
     case '*':
       Serial.println("*");
       break;
     case '0':
       Serial.println("0");
       break;
     case '#':
       Serial.println("#");
       break;
   }
 }
 //while(key = key)
 //Serial.println(key);
}


this

Code: [Select]

 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


PaulS

Quote
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!

Actually, it says that if key is not 0 (which is the value of NO_KEY), then, do stuff. Any key will result in the stuff being done.

Not that I like that style, mind you. I prefer explicit tests myself. Except in the case of pointers.

mr_hacker90



Code: [Select]

 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.

Code: [Select]

/*  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("#");
 }
}

CrossRoads

Follow the complete library example, save  the key value it is returned when a key is pressed
Code: [Select]

//add this
byte new_key;
void setup(){
// whatever you had
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    new_key = key;  //new_key is only updated when a key is pressed
  }
//then add your
//switch (new_key),
//or the slower if (key == '1') etc.

}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up