Pages: 1 [2]   Go Down
Author Topic: How to escape while control structure with implement of switch case  (Read 1195 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


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

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26466
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

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

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

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

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:
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:
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
« Last Edit: November 13, 2012, 02:01:33 pm by mr_hacker90 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
char stg[200] = "Short";
sizeof() will return 200
strlen() will return 5.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26466
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

These comments are not correct:

break; // supposed that break the case 1 if other case is being pressed

break; just tells the code to go the the closing } of  the section.  It does not stop anything.

If you want to stop ongoing code, you need to create an Interrupt when a key is pressed, and use that interrupt to stop the ongoing 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

These comments are not correct:

break; // supposed that break the case 1 if other case is being pressed

break; just tells the code to go the the closing } of  the section.  It does not stop anything.

If you want to stop ongoing code, you need to create an Interrupt when a key is pressed, and use that interrupt to stop the ongoing code.

understand. in order to stop the subroutine 1 under case 1 so that it can jump to case 2 for subroutine 2, i need to create an interrupt. for arduino uni, the interrupt in on pin 2 & pin 3. in your opinion, can these 2 pins handle interrupt for 12 case for keypad 3x4. i guess it not possible. by the way, my programming skill is just beginner. i just try & error. by doing that, i learn a little bit. i'm not studying programming language. this is just my interest.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26466
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

I think you could create an interrupt with 3 or 4 diodes - When keys are pressed, I think the Row or Column pin goes low - so put the anode on the row or columns, the cathodes all go to the interrupt pin with its internal pullup resistor enabled.
When any of the row (or column) keys is pressed, the diode goes low pulling the interrupt pin low.
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

I think you could create an interrupt with 3 or 4 diodes - When keys are pressed, I think the Row or Column pin goes low - so put the anode on the row or columns, the cathodes all go to the interrupt pin with its internal pullup resistor enabled.
When any of the row (or column) keys is pressed, the diode goes low pulling the interrupt pin low.


i got what your mean. either keypad column pins or row pins common connected to the diodes. but, due to the lack of programming language knowledge, i found it quite difficult. i guess i have to cheat to my mom. haha... before press keypad, need to press reset button first. haha...
Logged

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

by the way CrossRoads, do you have an example code for interrupt. i guess i should have a look first before i just give up. thank you, CrossRoads.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26466
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

Here's a combination of sleep & interruupt.

Code:

#include <avr/sleep.h>      // powerdown library
#include <avr/interrupt.h>  // interrupts library


int pin2 = 2;               // Int0 interrupt pin
byte sleep_flag;


//***************************************************
// *  Name:        pin2Interrupt, "ISR" to run when interrupted in Sleep Mode
void pin2Interrupt()
{
  /* This brings us back from sleep. */
}

//***************************************************
// *  Name:        enterSleep
void enterSleep()
{
  /* Setup pin2 as an interrupt and attach handler. */
  attachInterrupt(0, pin2Interrupt, LOW);
  delay(50); // need this?
  /* the sleep modes
   SLEEP_MODE_IDLE - the least power savings
   SLEEP_MODE_ADC
   SLEEP_MODE_PWR_SAVE
   SLEEP_MODE_STANDBY
   SLEEP_MODE_PWR_DOWN - the most power savings
   */
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // setting up for sleep ...
  sleep_enable();                       // setting up for sleep ...

    // Disable ADC
  ADCSRA &= ~(1 << ADEN);

  // Power down functions
  PRR = 0xFF;

  sleep_mode();                         // now goes to Sleep and waits for the interrupt

  /* The program will continue from here after the interrupt. */
  detachInterrupt(0);                 //disable interrupts
 
    // Power up functions
  PRR = 0x00;
 
  // Enable ADC
  ADCSRA |= (1 << ADEN);


  /* First thing to do is disable sleep. */
  sleep_disable();

  // then go to the void Loop()
}

// ***********************************************************************
// set up the pins as Inputs, Outputs, etc.
void setup()
{
  /* Setup the pin directions, write inputs High to turn on internal pullups */
  pinMode(pin2, INPUT);                 // our sleep interrupt pin
  digitalWrite(pin2, HIGH);

}                                            // end of void Setup()

// ***********************************************************************
// Main loop for reading the keypad and sending the button pushed out
// (with a unique address to be added eventually by reading 0-F from currently unused pins)

void loop()
{
// your code here to decide to go to sleep, lets say it sets a flag

if (sleep_flag == 1){
//    Serial.println("Sleep");               // for debug only

    enterSleep();                             // call Sleep function to put us out
                                              //  THE PROGRAM CONTINUEs FROM HERE after waking up in enterSleep()
  }                                           // end of checking to go to sleep


}                                             // end of void 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

Here's a combination of sleep & interruupt.

thank you very much. by the way, is that what i need to do right know is to combine the interrupt code with my original code right?
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26466
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

I don't know, you've got that other big section, if you do get an interrupt what will you do with in the middle of those?
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

that's the thing play in my head.. i need to interrupt all 12 cases.. seems to be impossible i think. n, when, interrupt, it will go to the respective case. let's say now in case 1, if i press keypad number 2, it will interrupt and goes to case number 2. the most problematic is 12 cases...
Logged

Pages: 1 [2]   Go Up
Jump to: