Hi there I want to use a very simple circuit just 4 leds and a button,
so the thing is that i want to turn them on and of in a sequence using millis() function
but i don´t know how to do it, the idea goes like so:
if (button_pressed)
{
1: (led_one, HIGH);
2: 200 milliseconds later (led_one, LOW) and (led_two, HIGH);
3: 200 milliseconds later (led_two, LOW) and (led_three, HIGH);
4: 200 milliseconds later (led_three, LOW) and (led_four, HIGH);
5: 200 milliseconds later (led_four,LOW);
}
I only want to initiate the led sequence if i press the button
so if i don´t press the button nothing happens.
Also i would like to use the pwm capabilities so i could adjust the brightness of each led instead of "HIGH or LOW" so it would be something like this:
if (button_pressed)
{
1: (led_one, 255);
2: 200 milliseconds later analogWrite(led_one, 0); and analogWrite(led_two, 255);
3: 200 milliseconds later analogWrite(led_two, 0); and analogWrite(led_three, 255);
4: 200 milliseconds later analogWrite(led_three, 0); and analogWrite(led_four, 255);
5: 200 milliseconds later analogWrite(led_four, 0);
the following code works just fine to sequence the leds in a infinite loop, but i cant make it work with the button
(i know is not the most elegant solution, so i´m open to other ways to do the same thing)
int one=11;
int two=10;
int three=9;
int four=6;
unsigned long previousMillis=0;
const long interval=200;
void setup(){///////////////////////////////////////////////////////////////////////
Serial.begin(9600);
pinMode(one,OUTPUT);
pinMode(two,OUTPUT);
pinMode(three,OUTPUT);
pinMode(four,OUTPUT);
}
void loop(){////////////////////////////////////////////////////////////////////////
unsigned long currentMillis=millis();
if((currentMillis-previousMillis>=interval)&&(currentMillis-previousMillis<=interval*2)){
analogWrite(one,255);
Serial.println("one");}
if((currentMillis-previousMillis>=interval*2)&&(currentMillis-previousMillis<=interval*3)){
analogWrite(one,0);
analogWrite(two,255);
Serial.println("two");}
if((currentMillis-previousMillis>=interval*3)&&(currentMillis-previousMillis<=interval*4)){
analogWrite(two,0);
analogWrite(three,255);
Serial.println("three");}
if((currentMillis-previousMillis>=interval*4)&&(currentMillis-previousMillis<=interval*5)){
analogWrite(three,0);
analogWrite(four,255);
Serial.println("four");}
if((currentMillis-previousMillis>=interval*5)&&(currentMillis-previousMillis<=interval*6)){
analogWrite(four,0);
previousMillis = currentMillis;}}
LED_SEQUENCE_WITHOUT_DELAY.ino (1.19 KB)