Change modes with button

Hello,

How can I make a button change program mode while the loop is running?

I want to do an RGB led cube, I want to change modes directy in loop… Have I to check the button input after every action?
As example:

(example, in finished code, there will be a LOT of actions)
//////////////////////////////////

while (mode == 1) {

while (fadeValue < 255) {

fadeValue++;
analogWrite(ledPin, fadeValue);
delay(5);

if (digitalRead(buttonPin) = HIGH) {
mode++;
}

}

}

while (mode == 2) {



}

//////////////////////////////////

Is there a trick that I don´t have to check the input every action?

Have I to check the button input after every action?

Yes.

Is there a trick that I don´t have to check the input every action?

Yes. Don’t use delay() (see the blink without delay example); use a state machine to keep track of what to do on this pass through loop() if the switch is not pressed.

Thank you :smiley:

while (mode = 1)

There's another trick to learn here too.

AWOL:

while (mode = 1)

There's another trick to learn here too.

...?

while (mode == 1)

Notice the difference?

Grumpy_Mike:

while (mode == 1)

Notice the difference?

:grin: I just wanted to make a general overview, it shouldn´t be right down to the smallest detail :grin:

It's a nice thought, but the compiler disagrees.

Your redaction missed one (though in this case, the compile wouldn't)

You also missed the code tags.