Pages: [1] 2   Go Down
Author Topic: How to escape while control structure with implement of switch case  (Read 1352 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi. hello everyone. i have a question here. how do i escape while control structure with implement of switch case. basically. i'm using 3x4 keypad. i want to display in serial monitor number 1 when i press keypad number 1 repeatedly until other number is being pressed. currently i managed to display number 1 until 0 in serial monitor. however, when i pressed the keypad, it just shows in serial monitor the number once only. i want to modify the program so that it will display the number repeatedly until the other number is pressed. here is the programming code. the keypad programming code is referred on the arduino playground. http://arduino.cc/playground/Main/KeypadTutorial and the while control structure is referred on the arduino reference http://arduino.cc/en/Reference/While. the program code i have modify already a little bit. but, seems no going as i planned. if i put the while control structure, the number that i pressed display repeatedly however it not break to other keypad numbers. in the programming code, i have made the change under case '1'. i also provide the screenshot of the serial monitor. managed to display number 1 repeatedly, however, did not break the while loop when the other keypad number is being pressed. your help, i really appreciate. thank you.

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


* Screen Shot.png (105 KB, 1360x768 - viewed 9 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Clear the vale of key after you use it, and use == for comparisons:
Code:
     case '1':
        while(key == '1'){
        Serial.println("1");
        key = 'z';  // or some invalid character        
        }
        break;
« Last Edit: November 13, 2012, 02:34:53 am by CrossRoads » Logged

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.

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.

Code:
key = '2';  // or some invalid character
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Something like:
Code:
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]
Logged

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.

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Something like:
Code:
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..

Code:
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

Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This may work as well

Code:
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;
    }
 ---------------- */
}
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

   Serial.println(key);
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1273
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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
Logged

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.

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

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.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2313
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Going back to the OP's original code

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

Code:
 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

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Follow the complete library example, save  the key value it is returned when a key is pressed
Code:
//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.

}
Logged

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.

Pages: [1] 2   Go Up
Jump to: