Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
If you want to outperform your peers about really immidiately aborting the sequence and switching off all leds as soon as the switch is turned to "OFF"
and you want to do this without non-blocking timing
You have to use a while-loop for the delaying where this while-loop includes checking the switch-positions all the time.
Only if you check the switch-positions all the time you can react immidiately on it.
This code-version shows the flaw of this approach.
It could be called
conditional aborting
each new part of the sequence does start with switching on an leds and is then immidiately aborted.
This is the reason why all leds light up and go off again very quick
For reliable operation it is not enough to wire the buttons to plus 5V.
As long as the button is not pressed the input is floating and will catch up electromagnetic noise
Only if you use inputmode INPUT_PULLUP the function works reliable
using INPUT_PULLUP inverts the logic
button pressed is LOW
button released is HIGH (because of the microcontroller-internally activated pullup-resistor)
This code uses INPUT_PULLUP and shows the ugly way of
conditional aborting which is let's say half-blocking but not really non-blocking
button-reading is done with this part of the code
// returns zero if all switches are switched OFF
// otherwise returns 1,2,3,4
byte readSwitches() {
byte result = 0;
for (byte Idx = 1; Idx <= 4; Idx++) {
if (digitalRead(Switch[Idx]) == pressed) {
result += Idx;
}
}
return result;
}
waiting is done with this part of the code
void waitWithSwitchChecking(unsigned long waitTime) {
unsigned long millisecCounter = 0;
switchState = readSwitches();
if (switchState == 0) {
return;
}
// switchState != 0 means a switch is ON
while ( (switchState != 0) && (millisecCounter < waitTime) ) {
switchState = readSwitches();
millisecCounter++; // increase counter each millisecond
delay(1); // wait a SINGLE millisecond
}
}
And this is the complete code which also has poor-mans debouncing through an initial delay(50)
Using poorman's debouncing is easier if you just put that delay() once in the loop function.
50 milliseconds shouldn't matter, and 20 milliseconds should suffice for all but the crappiest switches which you shoukd throw out immediately (in less than 1000 milliseconds) if you are trying to use them.
Placing it once in the loop is also directly the one place where a similar non-blocking method can be used, if @schwarz046 ever cares to do.
I'm sorry for misusing the word "immediately" here. I'm didn't mean that when the switch is turned off, the leds will be turned off without a single point of delay. What's happening in my code is that the leds will first finish the pattern of lighting before they turn off. What it should do is to stop without finishing the entire pattern first is the switches are turned off, it's okay for me to wait a single sec for it to turn off
this is good, except that when I try it in my set up. when I pressed switch no.2, it will run first the first pattern before running second pattern. I use dip switch btw not button
Without knowing if you changed the wiring from switch connected to +5V
to
switch connected to ground and using INPUT_PULLUP
it cannot be narrowed down why you observed that.
please post exactly that complete sketch with which you observed this behaviour.
In programming you have to be very precise
precise wiring
precise coding
precise describing