Go Down

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

mr_hacker90

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

CrossRoads

#1
Nov 13, 2012, 08:31 am Last Edit: Nov 13, 2012, 08:34 am by CrossRoads Reason: 1
Clear the vale of key after you use it, and use == for comparisons:
Code: [Select]

     case '1':
       while(key == '1'){
       Serial.println("1");
        key = 'z';  // or some invalid character        
        }
       break;
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.

mr_hacker90

Code: [Select]

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: [Select]

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

CrossRoads

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

Something like:
Code: [Select]

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

mr_hacker90


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

Something like:
Code: [Select]

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: [Select]

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


Go Up