Go Down

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

mr_hacker90



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.

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

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

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


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

mr_hacker90

#18
Nov 13, 2012, 07:58 pm Last Edit: Nov 13, 2012, 08:01 pm by mr_hacker90 Reason: 1
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, http://g33k.blogspot.com/2010/02/arduino-56x8-scrolling-led-matrix.html. 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().

Code: [Select]

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


Code: [Select]

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

PaulS

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

Code: [Select]
char stg[200] = "Short";
sizeof() will return 200
strlen() will return 5.

Go Up