I want to make LED blink patterns with multiple LEDs with the millis()
function. I researched about it but did not find a perfect solution.
I found one blog, but it is using a LED pin number array; I need a solution without an array. Any help will be appreciated.
This is my code which I have written:
const int Button_code_1 = 2;
const int Button_code_2 = 3;
const int Button_code_stop = 4;
const int LED_one = 6;
const int LED_two = 8;
const int LED_three = 10;
const int LED_four = 12;
int buttonState_code_1 = 0;
int buttonState_code_2 = 0;
int buttonState_code_stop = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Button_code_1,INPUT);
pinMode(Button_code_2,INPUT);
pinMode(Button_code_stop,INPUT);
pinMode(LED_Button_one,INPUT);
pinMode(LED_Button_two,INPUT);
pinMode(LED_Button_three,INPUT);
pinMode(LED_Button_four,INPUT);
pinMode(LED_one, OUTPUT);
pinMode(LED_two, OUTPUT);
pinMode(LED_three, OUTPUT);
pinMode(LED_four, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState_code_1 = digitalRead(Button_code_1);
Serial.println(buttonState_code_1);
buttonState_code_2 = digitalRead(Button_code_2);
Serial.println(buttonState_code_2);
delay(1000);
buttonState_code_stop = digitalRead(Button_code_stop);
if(buttonState_code_1==1)
{
pattern_one();
}
if(buttonState_code_2==1)
{
pattern_two();
}
}
void pattern_one()
{
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, HIGH);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, LOW);
delay(3000);
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, HIGH);
delay(3000);
digitalWrite(LED_one, HIGH);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, LOW);
delay(3000);
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, HIGH);
digitalWrite(LED_four, LOW);
delay(3000);
}
void pattern_two()
{
digitalWrite(LED_one, HIGH);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, LOW);
delay(3000);
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, HIGH);
digitalWrite(LED_four, LOW);
delay(3000);
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, HIGH);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, LOW);
delay(3000);
digitalWrite(LED_one, LOW);
digitalWrite(LED_two, LOW);
digitalWrite(LED_three, LOW);
digitalWrite(LED_four, HIGH);
delay(3000);
}
This is my sample code with four LEDs. I have written this code with delay()
, so when I want to change the pattern I can't because delay()
is blocking the code.
I researched this and found the millis()
function as the solution. How can I implement this code using the millis()
function?