How to escape while control structure with implement of switch case

Clear the vale of key after you use it, and use == for comparisons:

      case '1':
        while(key == '1'){
        Serial.println("1");
        key = 'z';  // or some invalid character        
        }
        break;
key = 'z';  // or some invalid character

if i use invalid character, the number 1 will not display continuously. same goes also if i use valid character like number 2.

key = '2';  // or some invalid character

Ah - in that case, when you get a valid key, save it & do the continuous switching on that.

Something like:

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  { new_key  = key;}

    switch (new_key)
    {
[code]
and make the }s match up at the end again.

[/code]

CrossRoads:
Ah - in that case, when you get a valid key, save it & do the continuous switching on that.

Something like:

void loop()

{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  { new_key  = key;}

switch (new_key)
    {

[code]
and make the }s match up at the end again.


[/code]

something like this right? but not working also..

void loop()
{
  char key = kpd.getKey();
  char new_key;
  if(key)  // Check for a valid key.
  {
    {new_key  = key;}
    switch (new_key)
    {
      case '1':
        while(key == '1'){    // while key 1 is pressed, it will keep repeatedly display number 1.
        Serial.println("1");
        key = 'Z';  // or some invalid character
        }
        break;    // supposed that break the case 1 if other case is being pressed

This may work as well

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

  if (!key)  // Check for a valid key.
     key = oldkey;

   Serial.println(key);

/* -------- This code not needed if all we want is to print the key

    switch (key)
    {
      case '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;
    }
 ---------------- */
}

haha... yeap.. it loop continuously however, did not not display the number respective to the keypad pressed... the serial monitor blank..

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

  if (!key)  // Check for a valid key.
     key = oldkey;

   Serial.println(key);
}

Oops! You need to have an else that saves the key to oldkey if the key is valid. This remembers it for next time.

Try this. I don't think you need the while's - if a key press is not received, just switch based on the last key that was received.

void loop()
{
key = kpd.getKey();
 if(key)  // Check for a valid key.
  {
    new_key  = key;
  }
 switch (new_key)
    {
      case '1':
                Serial.println("1");
        break;    // supposed that break the case 1 if other case is being pressed
:
:
} // end switch
} //end loop

thank you. but, while i want to compile it, it shows error. mentioned that new_key was not declared. so, i declared the new_key
the programming code is like below. but, it just display once only. not repeatedly until the other key is pressed.

void loop()
{
  char key = kpd.getKey();
  char new_key;
  
  if(key)  // Check for a valid key.
  {
    new_key  = key;
  }

    switch (new_key)
    {
      case '1':
        Serial.println("1");
        break;    // supposed that break the case 1 if other case is being pressed

  } // end switch
} //end loop

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.

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.

Going back to the OP's original code

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

  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

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.

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

Follow the complete library example, save the key value it is returned when a key is pressed

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

}

CrossRoads:
Follow the complete library example, save the key value it is returned when a key is pressed

i believe the code should be like this. but, the output on serial monitor is still like the original. only display the number correspond to the keypad number only once. perhaps that my code is somewhere wrong. but, i follow your instruction above.

/*  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 );

byte new_key;

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

void loop()
{
  char key = kpd.getKey();
  
  if (key != NO_KEY){
  new_key = key;  //new_key is only updated when a key is pressed
  }
  
  if(key)  // Check for a valid key.

    switch (new_key)
    {
      case '1':
        Serial.println("1");
        break;    // supposed that break the case 1 if other case is being pressed
      case '2':
        Serial.println("2");
        break;
  } // end switch
} //end loop

Take this line out

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

The line above it has already read the key in if one was pressed.

CrossRoads:
Take this line out

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

The line above it has already read the key in if one was pressed.

CrossRoads... it works... thank you very much...

it works well with Serial.println. however, it not works if calling a function or subroutine.. it did not break the first case if i pressed number 2 for 2nd case. i think it related with the subroutine itself. the subroutine itself is continuously repeated.. not stop or single like Serial.println. basically, i want to add up a little bit on Hari's Arduino Scrolling 56x8 LED matrix. http://arduino.cc/forum/index.php/topic,8672.0.html. the hardware part already done on PCB. based on hari's blog, Toys + Me = Fun.. i only made 1 modification which i add up a keypad. as a present to my mom. the keypad with serial monitor is basic start. i just want to get the idea how to do the keypad. now, when i merge it with the original code, it does display on LED matrix the first case which i pressed. but, did not break to second case if i pressed keypad number 2. below is the code. i do believe the root of the problem is under subroutine void AlphabetSoup1().

void AlphabetSoup1()
{
  char msg[] = "GOOD MORNING MOM   ";
  
  for (int charIndex=0; charIndex < (sizeof(msg)-1); charIndex++)
  {
    int alphabetIndex = msg[charIndex] - '@';
    if (alphabetIndex < 0) alphabetIndex=0;
    
    //-- Draw one character of the message --
    // Each character is only 5 columns wide, but I loop two more times to create 2 pixel space betwen characters
    for (int col = 0; col < 7; col++)
    {
      for (int row = 0; row < 8; row++)
      {
        // Set the pixel to what the alphabet say for columns 0 thru 4, but always leave columns 5 and 6 blank.
        bool isOn = 0; 
        if (col<5) isOn = bitRead( alphabets[alphabetIndex][col], 7-row ) == 1;
        Plot( numCols-1, row, isOn); // We ALWAYS draw on the rightmost column, the shift loop below will scroll it leftward.
      }
      
      //-- The more times you repeat this loop, the slower we would scroll --
      for (int refreshCount=0; refreshCount < 10; refreshCount++)
        RefreshDisplay();

      //-- Shift the bitmap one column to left --
      for (int row=0; row<8; row++)
      {
        for (int zone=0; zone < numZones; zone++)
        {
          // This right shift would show as a left scroll on display because leftmost column is represented by least significant bit of the byte.
          bitmap[row][zone] = bitmap[row][zone] >> 1;
          
          // Roll over lowest bit from the next zone as highest bit of this zone.
          if (zone < maxZoneIndex) bitWrite(bitmap[row][zone], 7, bitRead(bitmap[row][zone+1],0));
        }
      }
    }
  }
}
void loop()
{
  char key = kpd.getKey();
  
  if (key != NO_KEY){
  new_key = key;  //new_key is only updated when a key is pressed
  }
  
    switch (new_key)
    {
      case '1':
        AlphabetSoup1();
        break;    // supposed that break the case 1 if other case is being pressed
      case '2':
        AlphabetSoup2();
        break;
  } // end switch
} //end loop
  for (int charIndex=0; charIndex < (sizeof(msg)-1); charIndex++)

You should not be using sizeof() to determine the length of a string. That is what the strlen() function is for.

char stg[200] = "Short";

sizeof() will return 200
strlen() will return 5.

These comments are not correct:

break; // supposed that break the case 1 if other case is being pressed

break; just tells the code to go the the closing } of the section. It does not stop anything.

If you want to stop ongoing code, you need to create an Interrupt when a key is pressed, and use that interrupt to stop the ongoing code.