Two LEDs with seperate timings - how?

Hi all,

a real newbie here...

I’m wanting to blink two LEDs independently of one another:

LED 1: on = 801 milliseconds, off = 709 milliseconds
LED 2: on = 709 milliseconds, off = 801 milliseconds

They start off seeming to be synchronised, and slowly as time progresses they separate from one another, and then after a while they synchronise again… and then separate… then synchronise… ad infinitum.

What would be the code to do this?

Thanks in advance!
Josy

Blink without delay

You will need to adjust the code for 2 LEDs so that the on/off periods are different but it is not hard to do.

Two LED state machine

I agree with both other comments on here, but. I can only say one thing: Delay times!!
for example, the IDE example "blink". the code itself contains delay time, which can be adjusted to make it go faster or slower, but what I think you are looking for is something like a police lights effect?
which can be found here:

ofcourse you can adjust both the circuit and code to your liking!

kind regards,
-Goldfile

Nah, don't even think of doing it with delays.

Watch jasmine's video, understand a state machine, and you're set for life.

hey bud.

Can we have a link to Jasmine's video?

Goldfile:
hey bud.

Can we have a link to Jasmine's video?

A few replies up ^ there

hey,
ohh haha, I did not see that. my apologies :stuck_out_tongue:

kind regards,
-Goldfile

Thanks guys,

I’m finding the blink without delay code quite complicated to understand – where to put the times for how long it should be off and on for – (the video seems like it might be helpful, but I will need to watch it many more times to understand it). I'll keep with it - at least just for future projects.

Nick Gammon’s second code seems simpler to understand – would this work ad infinitum to create the effect in my initial question?
(Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs)

// Example of flashing multiple LEDs at different rates without delays
// Author: Nick Gammon
// Date: 23 December 2012

#include <LedFlasher.h>

// set up some LEDs
LedFlasher floodLight (8, 200, 300);
LedFlasher shuttleBayDoors (9, 300, 600);
LedFlasher impulseEngine (10, 900, 100);
LedFlasher strobe (11, 500, 1000);
LedFlasher navigation (12, 1000, 2000);
LedFlasher torpedoes (13, 250, 500);

void setup()
{
floodLight.begin ();
shuttleBayDoors.begin ();
impulseEngine.begin ();
strobe.begin ();
navigation.begin ();
torpedoes.begin ();
} // end of setup

void loop()
{
// update lights
floodLight.update ();
shuttleBayDoors.update ();
impulseEngine.update ();
strobe.update ();
navigation.update ();
torpedoes.update ();

// do other useful stuff here ...

} // end of loop

Thanks in advance once again,

(and Goldfile - I haven't had the chance yet to try to get my head around your police lights effect, but will try later; thank you also... :slight_smile:

Josy

hey!
no worries! I hope you like arduino as much as I do!
good luck in future projects!

kind regards,
-Goldfile

I’m finding the blink without delay code quite complicated to understand

It has about four significant lines of code.

  unsigned long currentMillis = millis(); // get the current time.
 
  if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;       // save the last time you blinked the LED 

    if (ledState == LOW)     // if the LED is off turn it on and vice-versa:
   // do something

One important thing to note is that as it stands, it is capable of flashing only one LED, but because "interval" is a variable, when you change the state of the LED, you could also change the on or off time too.

(one other thing currentMillis - previousMillis > interval should be currentMillis - previousMillis >= interval

Nick Gammon’s second code seems simpler to understand

That's because this part....

#include <LedFlasher.h>

.... basically does all the work for you.

These lines...

LedFlasher floodLight (8, 200, 300);
LedFlasher shuttleBayDoors (9, 300, 600);
LedFlasher impulseEngine (10, 900, 100)

.... all use LedFlasher() which is a function created inside LedFlasher.h, and the include makes that function available to you. It wouldn't surprise me in the slightest if Nick's LedFlasher internally used state machines to do the work. So I'm pretty sure, Nick's approach will be similar to the video, but insulating you from the drudge by bringing it to you in an include.

I have a MultiBlink sketch at my code repository (link below) that should do what you need, including effects like police lights, just by changing the parameter data at the top of the sketch.