How to escape while control structure with implement of s

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

How to escape while control structure with implement of s

Postby mr_hacker90 » Tue Nov 13, 2012 2:19 pm

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);
}
Attachments
Screen Shot.png
mr_hacker90
Newbie
 
Posts: 9
Joined: Mon Dec 12, 2011 11:16 pm

Re: How to escape while control structure with implement of

Postby shahrul » Tue Nov 13, 2012 4:18 pm

I'm not familiar with Arduino keypad library. Maybe you need to delete " if(key) " in your program.
Also you can do read keypad on your own ex Keypad Lesson.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: How to escape while control structure with implement of

Postby Alen.ATLow » Thu Nov 15, 2012 3:57 pm

switch (key)
{
case '1':
while(key == '1'){
Serial.println("1");
key = kpd.getKey();
}
break;
Alen.ATLow
Novice
 
Posts: 17
Joined: Mon Nov 12, 2012 12:53 pm


Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 54 guests