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