// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12=0;
unsigned long previousMillisLED13=0;
unsigned long previousMillisLED3=0;
// different intervals for each LED
int intervalLED12 = 500;
int intervalLED13 = 5000;
int intervalLED3 = 2000;
// each LED gets a state varaible
boolean LED13state = false; // the LED will turn ON in the first iteration of loop()
boolean LED12state = false; // need to seed the light to be OFF
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(3, OUTPUT);
}
void loop() {
// get current time stamp
// only need one for both if-statements
unsigned long currentMillis = millis();
// time to toggle LED on Pin 12?
if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
LED12state = !LED12state;
digitalWrite(12, LED12state);
// save current time to pin 12's previousMillis
previousMillisLED12 = currentMillis;
}
// time to toggle LED on Pin 13?
if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
LED13state = !LED13state;
digitalWrite(13, LED13state);
// save current time to pin 13's previousMillis
previousMillisLED13 = currentMillis;
}
}
My questions:
Didn't see the led12 and led13 have 10 times defferency, almost same intervals, why?
How can I use
millis()
as simple as delay?Is that a must to use if?
1 Answer 1
Your Timing is working, you can test it with the Serial monitor. So your problem is at some other place. Using delay() is from my perspective not a good solution, you did it better the way you already made.
For testing your sketch I included the communication for the Serial Montior (I had to comment out the LED activation since this is working in a different way on my board, and I also made the intervalls Shorter to see better the effect within the test sketch). Here you find the necessary changes and the output of the Serial Monitor.
My recommendation is to look with a simple Sketch if you are able to switch on/off your LEDs sperately without Timing to come closer to your problem. I dont think it is the Timing/use of millis().
#include <M5Stack.h> // needed for my board
// each "event" (LED) gets their own tracking variable
unsigned long previousMillisLED12 = 0;
unsigned long previousMillisLED13 = 0;
unsigned long previousMillisLED3 = 0;
// different intervals for each LED
int intervalLED12 = 50;
int intervalLED13 = 500;
int intervalLED3 = 2000;
// each LED gets a state varaible
boolean LED13state = false; // the LED will turn ON in the first iteration of loop()
boolean LED12state = false; // need to seed the light to be OFF
void setup() {
Serial.begin(9600);
// pinMode(13, OUTPUT);
// pinMode(12, OUTPUT);
// pinMode(3, OUTPUT);
}
void loop() {
// get current time stamp
// only need one for both if-statements
unsigned long currentMillis = millis();
// time to toggle LED on Pin 12?
if ((unsigned long)(currentMillis - previousMillisLED12) >= intervalLED12) {
LED12state = !LED12state;
// digitalWrite(12, LED12state);
// save current time to pin 12's previousMillis
previousMillisLED12 = currentMillis;
}
// time to toggle LED on Pin 13?
if ((unsigned long)(currentMillis - previousMillisLED13) >= intervalLED13) {
LED13state = !LED13state;
// digitalWrite(13, LED13state);
// save current time to pin 13's previousMillis
previousMillisLED13 = currentMillis;
}
Serial.print("LED12: ");
Serial.print(LED12state);
Serial.print(" LED13: ");
Serial.println(LED13state);
}
-
Thank you RJPlog, I'll test it.laoadam– laoadam2019年03月22日 16:08:33 +00:00Commented Mar 22, 2019 at 16:08
currentMillis - previousMillisLED12
is anunsigned long
. Explicitly casting it tounsigned long
is redundant.