Switch Case Help Needed

I'm using switch case to read a button input and display different messages on my LCD. When my switch case statement is showing one of the messages, I would like to be able to press and hold a different button for three seconds that clears the screen and prompts the user with something else.

Note* the words Replay() are functions that I have not copied here to save space and confusion. Each function just clears the screen and shows something else. My entire project is really big and I would like to avoid copying it all here to keep it simple. Thanks for any help!

Here's the part of the project in question:

switch (state){
                                                        case 0:
                                                        Replay0();
                                                        break;
                                                        case 1:
                                                        Replay1();
                                                        break;
                                                        case 2:
                                                        Replay2();
                                                        break;
                                                        case 3:
                                                        Replay3();
                                                        break;
                                                        case 4:
                                                        Replay4();
                                                        break;
                                                        case 5:
                                                        Replay5();
                                                        break;
                                                        default:
                                                        ;
                                                    }

We will have to assume none of your code is blocking as you have not shared it. In loop() monitor for a switch change to HIGH for 3 seconds, if you get one, set your new switch state that you want.

.

Do you mean something like this:

if (digitalRead(button)==HIGH);
case = X;

Can I use an if statement to change the Case?

Thanks

if(digitalRead(button)==HIGH) { state = the number you want; }

But you need to add code to make sure 3 seconds have gone by.

.

See Nick's SwitchManager library: The library can be downloaded from: http://gammon.com.au/Arduino/SwitchManager.zip .

are you using a 2x16 display with the built in keypad?

if so this link may help.

http://forum.arduino.cc/index.php?topic=329761.0

Sorry, I’m having a difficult time asking the question the right way while avoiding copy and pasting the 8600 bytes of code. What I am trying to do is make it so that when my switch case is displaying the item it should be, when I press a button it allows me to erase what is showing up on the lcd to simply see something else. No matter what I do, I cannot get the lcd to show the words “Something Else” when I press the button to high or low. It only ever shows “Total time blah blah”. Am I making a major rookie mistake here or does digitalRead not work inside of switch case?

switch (state){
case 0:
if (digitalRead(setButton)==HIGH){
Clear();
lcd.print (“Total Run Time:”);
line2();
lcd.print(“136 Hours”);
if (digitalRead(setButton)==LOW){
Clear();
lcd.print(“Something Else”);}}
break;
case 1:
Clear();
lcd.setCursor(0,0);
lcd.print(“Air Filter In:”);
nextAirfilterVal = eeprom_read_dword(&airFilterschedule) - eeprom_read_dword(&lastAirfilter);
nextAirfilterVal2 = (nextAirfilterVal);
lcd.setCursor(1,1);
lcd.print(nextAirfilterVal2,1);
lcd.print(" Hours");
break;
case 2:
Replay2();
break;
case 3:
Replay3();
break;
case 4:
Replay4();
break;
case 5:
Replay5();
break;
case 6:
Replay6();
break;
case 7:
default:
;

}
lastState = state;}
}

No matter what I do, I cannot get the lcd to show the words “Something Else” when I press the button to high or low.

How can setButton be HIGH and LOW at the same time? :wink:

case 0:
  if (digitalRead(setButton)==HIGH)   //<<<<<<<<<<<<<< HIGH
  {
    Clear();
    lcd.print ("Total Run Time:");
    line2();
    lcd.print("136 Hours");
    if (digitalRead(setButton)==LOW)  //<<<<<<<<<<<<<<< LOW
    { 
      Clear();
      lcd.print("Something Else");
    }
  }
  break;

if (digitalRead(setButton)==LOW)
Is inside of:
if (digitalRead(setButton)==HIGH)
{. . .}
so it will never evaluate.

Edit:
Watch where you put your statements. { { } }
Maybe you wanted to use “else” placed outside of the first “if” section.

Yeah that was a last ditch effort to get it to work. I tried it the right way first, but no luck.

case 0:
if (digitalRead(setButton)==HIGH){//you opened the switch here
Clear();
lcd.print ("Total Run Time:");
line2();
lcd.print("136 Hours");
if (digitalRead(setButton)==LOW){
Clear();
lcd.print("Something Else");}}//you closed it here
break;
case 1: 
Clear();

//how can it pass the first if and change inbetween 
//thats like saying in the next millisecond after the high are you low





case 0:
if (digitalRead(setButton)==HIGH){//you opened the switch here
Clear();
lcd.print ("Total Run Time:");
line2();
lcd.print("136 Hours");}//ok i closed it here
//if (digitalRead(setButton)==LOW){   this would work but not really required

//if it failed the first 
// i can just use else 
else{
Clear();
lcd.print("Something Else");}// i closed the else here
break;
case 1: 
Clear();

this might not work as we have no idea how the buttons are connected so we can not confirm that LOW is LOW as excepted by the input pin

As I said, maybe you want to use else but we cannot know exactly what you are trying to do.

CTRL T is your friend!

BTW look at how your code is formatted and how this example is formatted. Which is more readable?

case 0:
  if (digitalRead(setButton)==HIGH)
  {
    Clear();
    lcd.print ("Total Run Time:");
    line2();
    lcd.print("136 Hours");
  }
  else
  {
    Clear();
    lcd.print("Something Else");
  }
  break;

Thanks for the tip on keeping the code cleaner! I think I've been so obsessed trying to fix this seemingly easy problem that I've let my code turn into spaghetti. I tried your revised code but no luck. Can't seem to figure out what the heck is going on. It only shows "Total run time" and no matter how I press or hold the button it never shows "something else".

Before you go any further confirm the switch does work.

.

How is the switch wired? Make sure you have a PULLUP or you are using INPUT_PULLUP

Switch works. Its actually a membrane overlay connected via FPC to a circuit board, so there's no wires to troubleshoot. I double checked anyways though just to be sure.

Ok Is it similar to this? +5 ---- PULLUP ---- switch lead AND Pin ---- other switch lead ---- GND OR +5 ---- switch lead ---- other switch lead AND Pin ---- pulldown ---- GND

My board is 3.3V to 10K resistor to switch lead AND Pin to GND

So not push is high and pushed is low and you confirmed this with a meter?