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_ALL_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);
}