Arduino Button Code Switching

That was just for test. I don't need 1000ms delay, maximum 100ms delay for LED strips is more than fine for me. I add case 3 in following code line but case 3 doesn't work. Only 1 and 2 in operation. Here's code:

const byte buttonPin = 2;
byte mode = 0;
const byte maxMode = 1;
byte currentState = HIGH;
byte prevState = HIGH;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
}

void loop()
{
  currentState = digitalRead(buttonPin);
  if (currentState == LOW && prevState == HIGH)  //button became pressed
  {
    mode++;
    if (mode > maxMode)
    {
      mode = 0;
    }
    Serial.println(mode);
  }
  prevState = currentState;
  switch (mode)
  {

case 0:
Serial.println("running code for mode 0");

digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(50);
break;

case 1:
Serial.println("running code for mode 1");

digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(100);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(100);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(100);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(100);
break;

case 2:
Serial.println("running code for mode 3");

digitalWrite(5, HIGH);
delay(50);
digitalWrite(5, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6, LOW);
delay(50);
digitalWrite(7, HIGH);
delay(50);
digitalWrite(7, LOW);
delay(50);
digitalWrite(8, HIGH);
delay(50);
digitalWrite(8, LOW);
break;
}
}

Because

I think there is a delay but why I din't get it.

this one solved too, thanks.
the last thing reamain is mode delay!

Ok. Look at this code:

digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(100);
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
delay(100);
...
delay(100);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(6, LOW);
digitalWrite(8, LOW);
delay(100)

There are 12x delay(100) in that code. That is 1.2 seconds of delay in total. Where, in that code, is the button checked to see if it has been pressed?

Ok. What if I change these as LOW?

byte currentState = HIGH;
byte prevState = HIGH;

Try that if you like, you will see that it does not solve the problem.

What is the answer to my question?

Button check code I think is this:

currentState = digitalRead(buttonPin);
  if (currentState == LOW && prevState == HIGH)

Maybe, but is a bowl of dog doodoo. You continue to feed little bits of your requirements, which just stirs the pot. What is your project, what do you really want it to do, how is it wired?
It's way past time you stopped thrashing and read:
How to get the best out of this forum

Yes, that's correct. But where does that code appear in the lines of code in post #45?

I don't know. In the middle of the between 2 modes? What kind of code that I can put to avoid of this little delay?

Sorry, but you admit you don't know what you're doing, we don't know where you're going(due to a lack of info), you keep wanting snippet "solutions" (magic from God requests), and you're ignoring repeated requests for information (secret projects burnout). Have fun, bro!

You do know. The answer is: they don't appear there. And that's why there is a delay. And that's the problem with using delay() that @UKHeliBob was telling you about. Is it making sense yet?

Look at the lines of code I posted in post #45. The Arduino must execute them in that sequence, line 1 followed by line 2 followed by line 3 and so on until the last line, and do nothing else except that. It cannot escape that sequence. So the button is not checked for at least 1,200 milliseconds.

BTW, Arduino Nano is broken by short circuit. Diode is blown up I fixed it but it's temporary solution so there is no protection anymore. Because of this I have to done this project so quickly. Please lets done this asap and I will come back tomorrow for Arduino lessons. Okay?

Done. Muted.

Why did you ban me? I was asking for help here! I said and did nothing wrong. Why?

I get it now. So there is no solution to avoid this?
The other thing is modes are changes itself. 10k pulldown resistor is there.

I will stick with your code it's mainly working thanks for your help and patient.
The only problem is mode switching delay and sometimes modes switches by itself. @UKHeliBob

Banned, not banned, confused :woozy_face:

Yes, there is always a solution. But it is not easy.

I think there could be an easier way. It won't be perfect, but it will be easier than the long way.