Break in Switch (state) case does not work properly

Hello all,

In the following code i am trying to run a led pattern in a Switch (state) case but when i press the button to activate case 2 it does not do that but keeps blinking the red led. Sometimes it does switch after repeatedly pressing the button. I think i should better use a interrupt but i am not shure?!

#define button 4
#define redLed 0
#define greenLed 3
#define yellowLed 2

int state = 0;
int old = 0;
int buttonPoll = 0;






void setup() {
 
  pinMode(button, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);

  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
  digitalWrite(yellowLed, LOW);

}

void loop() {
  buttonPoll = digitalRead(button);
if(buttonPoll == 1) {
  delay(50);
  buttonPoll = digitalRead(button);
  if(buttonPoll == 0){
    state = old + 1;
  }
}
else{
  delay(100);
}
switch (state) {
  case 1:

    digitalWrite(redLed, HIGH);
    delay(1000);
    digitalWrite(redLed, LOW);
    delay(1000);
    digitalWrite(redLed, HIGH);
    delay(1000);
    digitalWrite(redLed, LOW);
    delay(1000);
    //digitalWrite(greenLed, LOW);
    //digitalWrite(yellowLed, LOW);
    old = state;
    break;
  case 2:
   
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    delay(1000);
    digitalWrite(greenLed, LOW);
    delay(1000);
    digitalWrite(greenLed, HIGH);
    delay(1000);
    digitalWrite(greenLed, LOW);
    delay(1000);
    digitalWrite(yellowLed, LOW);
    old = state;
    break;
  case 3:
  
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(yellowLed, HIGH);
    delay(1000);
    digitalWrite(yellowLed, LOW);
    delay(1000);
    digitalWrite(yellowLed, HIGH);
    delay(1000);
    digitalWrite(yellowLed, LOW);
    delay(1000);
    old = state;
    break;
  default:
 
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(yellowLed, LOW);
    old = 0;
  break;
    
}
}

Hi, @gjlp25
Welcome to the forum.
Thanks for using code tags. :+1:

You are only waiting 50ms for the button to open to get the state updated.

Google;

arduino button change of state

For some examples of what you are trying to do.

Adding Serial.print statements and using the IDE serial monitor will help you see how your variables are being used as your code runs.

Thanks.. Tom.. :smiley: :coffee: :australia:

Detecting a button press in your code depends detecting a press and release happening within 50 milliseconds but that press and release must happen in the brief period between states

However, there are 3 seconds in case 1 where the button will not be read and only a few microseconds when it will be so no wonder that the code misses keypresses

byte butLst;

void loop () {
    byte but = digitalRead (button);
    if (butLst != but) {
        butLst = but;
        delay (50);
        if (LOW == but){
            state = old + 1;
        }
    }

    switch (state) {
1 Like

2 problems with your code...

  • the logic to detect a button press is flawed. It only works when it detects a released button 50ms after detecting a pressed button.

  • the delay statements in the LED sequences mean that most of the time you will not be able to detect any button activity... because you are effectively blocked by the delay code.

There are 2 example sketches that will show you how do solve these problems..

  • StateChange Detection
  • BlinkWithoutDelay

1 Like

Thanks for your reply! I forgot to mention i am using a attiny85 so the serial monitor is off the table. And i cannot use softwareserial cause i do not have the rx and tx pins available

Software Serial on the ATtiny85 – The Wandering Engineer

Note: Sorry! I misread your post ... understand resource limitation on pin numbers!

too many delays in the code, every delay halts code execution

Wait a moment, the Wokwi simulator has a workaround to show messages to the Wokwi Serial Monitor. You can develop most of your sketch in Wokwi using that.
Here is your project in Wokwi: wait-a-moment.ino - Wokwi Arduino and ESP32 Simulator
It is your original sketch, as others already wrote: there are too many delays and the button detection has to be improved.

Thanks guys! That put's me on the right track!

Thank you. Sometimes i have tunnelvision while the solution is right under my nose

If you are using the ATTiny85 then I very strongly recommend this core... it is much better than others, and supports a lot of standard Arduino functionality... like Serial for example.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.