help with "switch case"

Can some one pls help me with this code.
I want to use a keypad for selecting "case" and i want it to repeate this case until another "case" is choosen.
I have the keypad working, but when pressing the buttons on the keypad, it only runs 1 time. Then it stops.

switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2 //repeat until another case ischoosen
break;
}

but when pressing the buttons on the keypad, it only runs 1 time.

So put the whole case statement in a loop.

Grumpy_Mike:

but when pressing the buttons on the keypad, it only runs 1 time.

So put the whole case statement in a loop.

Im new to the arduino, been trying to find a way to do this. How do i put the case in a loop?

Please attach all your code.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
//key pad conected to pins 22-28
byte rowPins[ROWS] = {3,2,1,0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,5,4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int timer = 175; // Higher number = longer scan.
int indLED = 175; //This is the delay on the meet in the middle sequence, updated to use an integer function
int var = 0;

//***********************************************************//
// digital Pins Outlets are hooked up to
int thisPin5 =7;
int thisPin6 =8;
int thisPin7 =9;
int thisPin8 = 10;
int thisPin9 = 11;
int thisPin10 = 12;
int thisPin11 =13;
int incomingByte;
//**********************************************************//

void setup()
{

// set digital pins as outputs to switch relays
pinMode(thisPin5, OUTPUT);
pinMode(thisPin6, OUTPUT);
pinMode(thisPin7, OUTPUT);
pinMode(thisPin8, OUTPUT);
pinMode(thisPin9, OUTPUT);
pinMode(thisPin10, OUTPUT);
pinMode(thisPin11, OUTPUT);

//for keypad
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

}

void loop()
{

////////////////////////////////////////////////////////////////////////////
//listen for keypad press
char key = keypad.getKey();
//print key to serial for communicate over serial
if (key)
{
Serial.println(key);

}

}

/////////////////////////////////////////////////////////////////////////////
// Void key events
//take care of some special events
void keypadEvent(KeypadEvent key)
{
switch (keypad.getState())
{
case PRESSED:
switch (key)
{
///////////////////////////////////////////////////////////////////////////////
case '1':
var = 0;
if(var < 10){
// loop from right to left:
for (int thisPin = 7; thisPin < 13; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}

// loop from left to right:
for (int thisPin = 12; thisPin >= 7; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
var++;
}
}
else

break;
///////////////////////////////////////////////////////////////////////////////
case '2':

break;
///////////////////////////////////////////////////////////////////////////////

}
}
}

At the top of loop, you update key every time:
//listen for keypad press
char key = keypad.getKey();
//print key to serial for communicate over serial
if (key)
{
Serial.println(key);

}

and then you try to switch:case on it.
Add some code to hold the value of key and switch on that, vs switching on key when no value has been returned.

CrossRoads:
At the top of loop, you update key every time:
//listen for keypad press
char key = keypad.getKey();
//print key to serial for communicate over serial
if (key)
{
Serial.println(key);

}

and then you try to switch:case on it.
Add some code to hold the value of key and switch on that, vs switching on key when no value has been returned.

Do you have a example on how to do this?

Do you have a example on how to do this?

Does that mean "can you do it for me"?
You are not going to learn like that.

Please read the how to use the forum sticky to learn how to post code correctly.

Some of this code looks very much like the Keypad example.
The way that I see it, there are three problems:

  1. The event listener should not be used.

  2. The switch case should be in this code, in place of Serial.println(key):

void loop()
{
  
  
   ////////////////////////////////////////////////////////////////////////////
  //listen for keypad press
  char key = keypad.getKey();
  //print key to serial for communicate over serial
  if (key) 
  {
    Serial.println(key);
 
  }
  
}
  1. keypad.getKey() may return an indication that there is no new keypress. Thus, this may work better (untested):
char oldKey ;

void loop()
{
  
  
   ////////////////////////////////////////////////////////////////////////////
  //listen for keypad press
  char key = keypad.getKey();
  
  if (key)  oldKey = key;
  switch (oldKey) 
  {
    case '1':
      //do something when key '1' was pressed                                                       //repeat until another case is choosen
      break;
    case '2':
      //do something when key '2' was pressed                                                      //repeat until another case is choosen
      break;
  }
}

Grumpy_Mike:

Do you have a example on how to do this?

Does that mean "can you do it for me"?
You are not going to learn like that.

Please read the how to use the forum sticky to learn how to post code correctly.
[/quote

No, it means i have no idea how to do it. That why i came to the forum.
Im stuck! I been trying to find a solution for this for days now.

Whenyou get a valid key, save it as a new name, and switch on that. keep that value until a new valid key comes in. vad4088's mod shows that.

I got the code almost to work. Im having trubble exiting the case. Any idea how to get around this problem?
Now the case just loops over and over. Need some way of exiting the case.

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char oldKey;
char keys[ROWS][COLS] = {
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'},
   {'*','0','#'}
   };
//key pad conected to pins 22-28
byte rowPins[ROWS] = {3,2,1,0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,5,4}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
//int oldKey = 0;
int timer = 175; // Higher number = longer scan.
int indLED = 175; //This is the delay on the meet in the middle sequence, updated to use an integer function 



//***********************************************************//
// digital Pins Outlets are hooked up to
int thisPin5 =7;
int thisPin6 =8;
int thisPin7 =9;
int thisPin8 = 10;
int thisPin9 = 11;
int thisPin10 = 12;
int thisPin11 =13;
int incomingByte; 
//**********************************************************//

void setup()
{
  
  
// set digital pins as outputs to switch relays
  pinMode(thisPin5, OUTPUT);
  pinMode(thisPin6, OUTPUT);
  pinMode(thisPin7, OUTPUT);
  pinMode(thisPin8, OUTPUT);
  pinMode(thisPin9, OUTPUT); 
  pinMode(thisPin10, OUTPUT);
  pinMode(thisPin11, OUTPUT);
  
  digitalWrite(thisPin5, HIGH);
  digitalWrite(thisPin6, HIGH);
  digitalWrite(thisPin7, HIGH);
  digitalWrite(thisPin8, HIGH);
  digitalWrite(thisPin9, HIGH);
  digitalWrite(thisPin10, HIGH);
  digitalWrite(thisPin11, HIGH);
  
  

//for keypad
//char oldKey ;
  }
  
  void loop()
{
  
  
///////////////////////////////////////////////////////////////////////////
//listen for keypad press

  char key = keypad.getKey();

  if (key)  oldKey = key;
  switch (oldKey) 
 
{
  case '1':

  // loop from right to left:
  for (int thisPin = 7; thisPin < 13; thisPin++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

  // loop from left to right:
  for (int thisPin = 12; thisPin >= 7; thisPin--) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
    }
   break;
   
  
///////////////////////////////////////////////////////////////////////////////  
  case '8':
  oldKey = 0;
  break;

    
            }
    }

A wise man once sent me here to learn about Switch Cases.

I know you're not dealing with Serial Data, but this is where I learned all about switch cases!

The original poster wrote "I want to use a keypad for selecting "case" and i want it to repeate this case until another "case" is choosen."

Now the original poster objects that the case repeats until another case is chosen - as requested.

What behavior is really wanted?

vaj4088:
The original poster wrote "I want to use a keypad for selecting "case" and i want it to repeate this case until another "case" is choosen."

Now the original poster objects that the case repeats until another case is chosen - as requested.

What behavior is really wanted?

It will not get out of "case" even if I choose another one. It just runs forerver.

So what happens when you enter a '2'?

You are only checking for two values, '1' and '8'. If you enter a '1' it should start flashing your LEDs and keep flashing them until you enter an '8'.

Does it do this? If not what does it do?

Add a serial print after this
char key = keypad.getKey();
see if it's even getting read.

Memnon:
It will not get out of "case" even if I choose another one. It just runs forerver.

No, it is badly written code but it will do what you want, you just have to keep your finger on the 8 key for about three seconds or longer so the code has time to see it.

Grumpy_Mike:

Memnon:
It will not get out of "case" even if I choose another one. It just runs forerver.

No, it is badly written code but it will do what you want, you just have to keep your finger on the 8 key for about three seconds or longer so the code has time to see it.

Ok. Do you have any tips on improving the code? What should i do to make it "better"?

Ok. Do you have any tips on improving the code? What should i do to make it "better"?
[/quote]

It really sounds like you need to learn about interrupts. You're trying to keep those lights flashing until another button is pressed, and keep them flashing in those sequences until you press another button, but your code states to run that code, then after the lights have flashed the sequence to look for a button press, but only shortly(the arduino cycles 16 million times per second). The bulk of the time, your code is running the blinking lights part.

You could also hold the button down until it changes, but interrupts will be your best bet.