Why is the code below the 2nd LED not blinking. How to solve that with the same milis values?
const int ledPin = LED_BUILTIN;
const int ledPin2 = D2;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long previousMillis2 = 0;
const long interval2 = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
if (milis() - previousMillis >= interval) {
previousMillis = milis();
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
if (milis() - previousMillis2 >= interval2) {
previousMillis2 = milis();
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin2, ledState);
}
}
PaulRB
January 30, 2023, 7:52pm
2
There are two variables for everything, except....
Yes, I want to fire 2 lights but different milis variables and same running time. That is what I want
Why is the code below the 2nd LED not blinking.
Because the code has serious errors.
Post code that compiles and uploads without errors, and tell us what goes wrong, if anything.
You've got two LEDs but only one ledState variable. That's probably not what you want....
This is such a common question on here several people got together and wrote a tutorial all about blinking multiple LEDs with millis()
Introduction
A common question is to ask how to flash 2 (or more) LEDs at the same time. Typically the person asking will be using delay and will be experiencing the main problem with delay: it blocks and stops anything else from happening.
This tutorial contains several different approaches to the problem of flashing 2 or more LEDs at the same time with independent timing for each LED. The basic principles for each can all be adapted for other situations requiring multiple timers working inde…
Millis() cannot 'collide' in any way.
Spelling: millis()
mike india lima lima india sierra
b707
January 30, 2023, 10:21pm
10
paulpaulson:
Spelling: millis()
because OP needs two different millis variables...
red_car
January 30, 2023, 10:58pm
11
Because this code does not compile.
...and even when it does you will need an ledState2 variable.
Think. How many time of day are there? One. It's all anyone needs to time as many things as they need. Similarly, you only need one millis().
Yes, I'll give it a try and find out why it can't create mailing lists at the same time, only with different variables. For example, you like the code below. So the led light will still be in the low position without blinking at all
unsigned long previousMillis1 = 0;
const long interval1 = 1000;
unsigned long previousMillis2 = 0;
const long interval2 = 1000;
void loop() {
if(millis() - previousMillis1 >= interval1) {
previousMillis1 = millis();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("test");
}
if (milis() - previousMillis2 >= interval2) {
previousMillis2 = milis();
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
red_car
January 31, 2023, 2:39am
15
bimosora:
you like the code below
No... because there is no such function as...
bimosora:
milis()
so it still won't compile. Do you understand why?
Is it because I implemented the milis() function without creating a variable?
Where did you implement the milis() function? Please post the code for it.
Or did you mean to use the millis() function, but spelled it incorrectly?
red_car
January 31, 2023, 4:36am
18
No, it's because you spelt mill is() wrong.
Posts #4 , #9 , #14 , #15 , #17 , and now #18 are all trying to tell you that.
Like this?
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
}
red_car
January 31, 2023, 8:32am
20
bimosora:
Like this?
Well you now have millis spelt correctly... so that's a start.
Does your full program now compile? Does it do what you want?