# issues with Switch cases

**URL:** <https://forum.arduino.cc/t/issues-with-switch-cases/509806>\
**Category:** Programming\
**Created:** [February 22, 2018, 12:59am UTC](https://forum.arduino.cc/t/issues-with-switch-cases/509806 "2018-02-22T00:59:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markusm1979](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@markusm1979](https://forum.arduino.cc/u/markusm1979)\
**Post date:** [February 22, 2018, 12:59am UTC](https://forum.arduino.cc/t/issues-with-switch-cases/509806/1 "2018-02-22T00:59:03Z")

</div>

What i am trying to achive is:

press button  
led on

press button again  
led blinking

press button again  
led blinking fast

press again  
led off

i have been using the case statements.  
What happens is that i press the button led goes on, then nothing changes until the led turns off in case 4.

Any help would be highly appreciated.

```cpp
#include <avr/sleep.h>

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin =  1;     

// the number of the LED pin

int ledState = LOW; 
unsigned long previousMillis = 0; 
const long interval = 1000; 
const long interval1 = 500; 
// variables will change:
      // variable for reading the pushbutton status

bool oldState = HIGH;
int showType = 0;
int Power __attribute__ ((section (".noinit")));

void PowerDown () {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA &= ~(1<<ADEN);      // Turn off ADC to save power
  sleep_enable();
  sleep_cpu();
}
  

void setup() {
 Power = !Power;
  if (!Power) PowerDown();
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // read the state of the pushbutton value:
bool newState = digitalRead(buttonPin);
 if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(buttonPin);
    if (newState == LOW) {
      showType++;
      if (showType > 4)
        showType=0;
      startShow(showType);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
 
  }
}
 // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){
    case 0: digitalWrite (ledPin, HIGH);
            break;
    case 1:  {

                  unsigned long currentMillis = millis();

                    if (currentMillis - previousMillis >= interval) {
                        // save the last time you blinked the LED
                        previousMillis = currentMillis;

                        // if the LED is off turn it on and vice-versa:
                    if (ledState == LOW) {
                        ledState = HIGH;
                          } else {
                        ledState = LOW;
                        }

                        // set the LED with the ledState of the variable:
                      digitalWrite(ledPin, ledState);
                        }
                        }
                        ;
            break;
    case 2: 
              {

                {
                    unsigned long currentMillis = millis();

                    if (currentMillis - previousMillis >= interval1) {
                    // save the last time you blinked the LED
                      previousMillis = currentMillis;

                    // if the LED is off turn it on and vice-versa:
                    if (ledState == LOW) {
                        ledState = HIGH;
                    } else {
                        ledState = LOW;
                            }

                    // set the LED with the ledState of the variable:
                    digitalWrite(ledPin, ledState);
                  } 
                }
            }
  
    ;
            break;
    case 3:  {
                        static float in = 4.712;
                        float out;
 
                        // do input, etc. here - as long as you don't pause, the LED will keep pulsing
  
                        in = in + 0.001;
                        if (in > 10.995)
                        in = 4.712;
                        out = sin(in) * 127.5 + 127.5;
                        analogWrite(ledPin,out);
                      }
                    ;
    
            break;
    case 4: digitalWrite (ledPin, LOW);
            break;
   
  }
}

```

---

<div class="post-metadata">

**Author:** ![markusm1979](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@markusm1979](https://forum.arduino.cc/u/markusm1979)\
**Post date:** [February 22, 2018, 1:33am UTC](https://forum.arduino.cc/t/issues-with-switch-cases/509806/3 "2018-02-22T01:33:03Z")

</div>

Hello Delta  
Thank you for your help.  
Sorry for my lack of knowledge, but how do i check the mills inside the case statement?

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [February 22, 2018, 2:34am UTC](https://forum.arduino.cc/t/issues-with-switch-cases/509806/4 "2018-02-22T02:34:30Z")

</div>

You need to include something called 'State Change Detection'. See the IDE example by the same name: File/examples/digital/stateChangeDetection. That is, sensing when the switch \*goes \* low. This will be true for only one pass through the code.

Then, outside the switch detection code you make something like:

```cpp
if(switchInputJustWentLow){ // this way everything within only happens once per button press
  showType++;
  if (showType > 4){
    showType=0;
  }
  startShow(showType);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:01pm UTC](https://forum.arduino.cc/t/issues-with-switch-cases/509806/5 "2021-05-05T23:01:34Z")

</div>

Closed on May 5, 2021, 11:01pm UTC


