Hello! I'm new with this forum and Arduino. Currently I'm working with a project with 4 LEDs and a button. The point is, when you're pressing the button, LEDs are turning off and on (first LED is on,then it goes off, then second LED is on, then goes off...). When I press the button, it's working (LED's are turning on and off), but when I release the button, LEDs will go off but only when the former command is completed (for example, when I release the button while the second LED is off, first the third LED will go on and off, then fourth and after that all LEDs will go off). I don't know how to fix this, so if anyone could help me, I will be grateful.
One of the first thing you should try out is blinking 2 or more LEDs at different frequencies. This can only be accomplished with a non-blocking state machine, so the usefulness and the purpose of a millis() based timer becomes self-evident.
Six or more LEDs with periods in the range of 200-500 ms, but with small differences of 1 or 2 ms, make interesting chasing patterns if you watch them go for a few minutes.
Hello
This task seems to be a scholl assigment.
Take some time and make a search inside the forum.
I guess you will find several solutions.
Have a nice day and enjoy coding in C++.
Hi guys. First of all, thanks for the all replies and tips. I managed to learn some basics of millis, but I don't know how to do this. I had an idea, but it's not working . LEDs are on when I press, but they're not turning on and off. When I release the button, they're off. That part is working. Here's the code:
#include <Bounce2.h>
int pin[4] = {7, 6, 5, 4};
int btn = 8;
int dt = 250;
int pt = 0;
int state = LOW;
Bounce b = Bounce();
void setup() {
b.attach(btn, INPUT);
b.interval(250);
pinMode(pin[0], OUTPUT);
pinMode(pin[1], OUTPUT);
pinMode(pin[2], OUTPUT);
pinMode(pin[3], OUTPUT);
}
void loop() {
int g = digitalRead(btn);
int ct = millis();
if(g==0){
digitalWrite(pin[0],HIGH);
if(ct-pt>=dt){
pt=ct;
digitalWrite(pin[0],LOW);
}
digitalWrite(pin[1],HIGH);
if(ct-pt>=dt){
pt=ct;
digitalWrite(pin[1],LOW);
}
digitalWrite(pin[2],HIGH);
if(ct-pt>=dt){
pt=ct;
digitalWrite(pin[2],LOW);
}
digitalWrite(pin[3],HIGH);
if(ct-pt>=dt){
pt=ct;
digitalWrite(pin[3],LOW);
}
}
if(g==1){
digitalWrite(pin[0],LOW);
digitalWrite(pin[1],LOW);
digitalWrite(pin[2],LOW);
digitalWrite(pin[3],LOW);
}
}
Everyone's lives, including yours in a few months' time when you try to make sense of old code, will be a lot simpler if you use words like currentTime and so on for variable names.
You're not using the bounce library properly- in fact I don't think you're using it at all.
Have a look at the examples that come with the library... you need to do a bounce.update() each time through loop(), and also you shouldn't do a normal digitalRead() any more... the example has a line like this which replaces the normal digitalRead():
Hello
All in all your sketch needs a time- and button handler for the led sequencer and debouncing.
C++ provides a lot smart instruction eg "struct" to built logical groups for the usage of User Defined Datatypes (UDT). The LED sequencer could be one of these contatining the timing and the ON/OFF sequence to be executed by a how ever contolled timer function.
Have nice day and enjoy coding in C++.
Does this approach suit your needs? It reacts in a timely manner as you need, and resets when the button is released. It is using delay not millis() though, so even though it reacts as if it's not-blocking, if you add more code it will be at the mercy of the two 250ms delays: