Hi everyone, I'm programming a program to start a sequence with five leds using a push button then turn off the leds with the same push button, I'm making the sequence with cycle for but when I push again leds don't turn off
I moved your topic to an appropriate forum category @8juancho.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Due to your history of forum rule violations, I am giving you a short account suspension. I hope that when you return to the forum you'll be more respectful of our community.
Follow the execution of your loop line by line and think about it. It reads the button, but then it gets to the for loop. Inside the for loop nothing reads the button and at the end of the for loop it goes back to the beginning of the for loop. SO for that entire duration, you've not coded for anything involving that button.
Don't block with a for loop. Let the loop function be the loop. Use if statements and variables to keep count and work out how to take one step at a time. Use millis to see how long since the last step so you can decide if it is time to take another step as in the famous Blink Without Delay example sketch.
I assume that you have written the programme by yourself, then it is quite easy to find the error.
There's a trick to figuring out why something isn't working:
Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables and determine whether they meet your expectations.
Take a view here to check the proper wiring of the button.
look this over
need to make processing of leds and monitoring of buttons independent of one another more of an architecture than coding question (how do you make the different pieces work together)
const byte ButPin = A5;
const byte LedPin [] = { 8, 9, 10, 11, 12 };
const int Nled = sizeof(LedPin);
byte butState;
int flag;
int idx;
unsigned long msecLast;
enum { Off = HIGH, On = LOW };
void loop ()
{
unsigned long msec = millis ();
if (flag) {
if (msec - msecLast >= 500) {
msecLast = msec;
digitalWrite (LedPin [idx], ! digitalRead (LedPin [idx]));
if (Nled <= ++ idx)
idx = 0;
}
}
else {
for (int n = 0; n < Nled; n++)
digitalWrite (LedPin [n], Off);
}
byte but = digitalRead (ButPin);
if (butState != but) { // change
butState = but;
delay (20); // debounce
if (LOW == but) // pressed
flag = ! flag;
}
}
void setup ()
{
Serial.begin (9600);
pinMode (ButPin, INPUT_PULLUP);
butState = digitalRead (ButPin);
for (int n = 0; n < Nled; n++) {
pinMode (LedPin [n], OUTPUT);
digitalWrite (LedPin [n], Off);
}
}
The code in post #7 does switch on sequentially led 1,2,3,4,5 and then switch them OFF 5,4,3,2,1
But this code-version does not yet stop the sequence at any earlier point.
then full sequence has ran down.
I tried to write the code with this immidiately stopping using flags.
But it turned out to be pretty complicated at least with the aproach I tried to make it work.
But there is a much easier approach:
using a state-machine
You can imagine a state-machine as putting the device in different modes of operation
mode 1: with all LEDs off waiting for a buttonpress
if button is pressed switch to mode 2
mode 2: switch LEDs sequentially on 1,2,3,4,5 with delay 500 milliseconds
automatically switch to mode 3
mode 3: switch LEDs sequentially off 5,4,3,2,1 with delay 500 milliseconds
if sequence is finished switch automatically to mode 1
mode 4: switch all LEDs off immidiately without any (noticable) delay
then switch automatically to mode 1
the button is always checked for presses
with checking an additional condition
if (sequenceIsActive) {
switch to mode 4 (immidiately OFF)
if the sequence is active at any point simply change mode of operation to mode 4 (immidiately OFF)
this is done using the switch-case-break;-statement
The switch-case-break-statement reduces the if-conditions and flag-variables to a minimum because in each mode only that code that belongs to that mode of operation is executed. This means you simply don't need additional flags
These modes of operation are called "states".
Hence the name state-machine. This word was chosen from highly sophisticated theoretical informatic scientists but is etablished as the common name for it.
the approach i posted demonstrates a fairly simple approach (50 lines) using a single flag to indicate on/off and using millis() rather than delay() so that a button press can be detected immediately and turn things off