I'm pretty new to arduino and would like some help in getting my code to work. My goal is to have two LEDs turn on independently. The first LED will turn on for 300ms and then followed by the second LED that will turn on for 500ms.
The intervals that this sequence occurs is randomized meaning that there can be a large or short delay until the LED 1 goes on again (followed by LED2). I haven't gotten to this yet. I'm stuck on getting the LEDs to blink independently using millis().
I am using an arduino Uno. Thank you in advance for your assistance and help!
Please see code below:
const unsigned int onTime = 300;
const unsigned int offTime = 1000;
const unsigned int onTime1 = 500;
const unsigned int offTime1 = 500;
// Tracks the last time of event
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
// Interval is how long we wait
int interval = onTime;
int interval1 = onTime1;
// Used to track if LED should be on or off
boolean LEDstate = true;
boolean LED1state = true;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(13, LEDstate);
digitalWrite(12, LED1state);
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
// Change wait interval, based on current LED state
if (LEDstate) {
// LED is currently on, set time to stay off
interval = offTime;
LED1state = !(LED1state);
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
LEDstate = !(LEDstate);
LED1state = !(LED1state);
previousMillis = currentMillis;
}
}
OK, so you have one piece of code which is controlling both LEDs together. You need to duplicate that with each section controlling a different LED. You can use the same currentMillis, but need to use the different previousMillis and interval for each LED.
We tend to give you a first Karma point for posting correctly in code tags.
Thank you! The lights are blinking at different times. However, the LEDs are not coming turning on in the same order and it seems that the distance between the two lights is changing. Ideally, I would want the time between the LEDs to be fixed. I drew a (poorly) drawn picture in paint. arduino-project — ImgBB
Edit: Seems like this code makes it come on sequentially. Why does the LED on Pin 12 come on first?
const unsigned int onTime = 300;
const unsigned int offTime = 1000;
const unsigned int onTime1 = 500;
const unsigned int offTime1 = 800;
// Tracks the last time of event
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
// Interval is how long we wait
int interval = onTime;
int interval1 = onTime1;
// Used to track if LED should be on or off
boolean LEDstate = true;
boolean LED1state = false;
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(13, LEDstate);
digitalWrite(12, LED1state);
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
// Change wait interval, based on current LED state
if (LEDstate) {
// LED is currently on, set time to stay off
interval = offTime;
} else {
// LED is currently off, set time to stay on
interval = onTime;
}
LEDstate = !(LEDstate);
previousMillis = currentMillis;
}
if ((unsigned long)(currentMillis - previousMillis1) >= interval1) {
// Change wait interval, based on current LED state
if (LED1state) {
// LED is currently on, set time to stay off
interval1 = offTime1;
} else {
// LED is currently off, set time to stay on
interval1 = onTime1;
}
LED1state = !(LED1state);
previousMillis1 = currentMillis;
}
}
}