Multiple Blinking LEDS at different intervals.

Hi Arduino Forum. Been a while since I wrote. I'm now in a process of creating a senior high school project. It's an awarness reminder about how we are polluting our oceans with plastic. What it will consist of is three objects, a Plastic Bottle, a Plastic Bag, and a cube of Plastic each containing a LED inside them. Every time they flash, they will represent the following:

Flash Cube of Plastic every 3.73 seconds = 1 Tonne of plastic thrown away into the ocean.
Flash Plastic Bottle every 0.76 seconds = Equivalent of 10'000 plastic bottles thrown away.
Flash Plastic Bag every 1.53 seconds = Equivalent of 100'000 plastic bags thrown away.

This is a programming question not a project question since I would like to get a suggestion on how to flash them when each of them has a separate interval. Is there something beyond using the delay()? A modulo function perhaps?

Please dump this post with suggestions! :stuck_out_tongue: :cold_sweat: :sweat_smile:
Thank you! :slight_smile:

Move away from delay()... look at the Blink Without Delay sketch in the IDE: FIle> Examples> 02 digital

Oh, and don't forget Grumpy_Mike's state machine tutorial

Seeing as all three objects have the same basic behaviour I'd approach it with some class.

... and it would look a lot like blink without delay()

My MultiBlink example sketch (see code library link below) will do what you need. You just have to edit the data table at the top of the sketch with you pinout and timing parameters.

Master the “Blink Without Delay” technique.

const unsigned long TaskAtime  = 1865UL; //Runs TaskA 3730/2
const unsigned long TaskBtime  = 380UL;  //Runs TaskB 760/2
const unsigned long TaskCtime  = 765UL;  //Runs TaskC 1530/2

unsigned long TimeA;                     //Times up, Task A time
unsigned long TimeB;                     //Times up, Task B time
unsigned long TimeC;                     //Times up, Task C time
// etc.

void setup()
  TimeA = millis();                      // 
  TimeB = TimeA;                         //
  TimeC = TimeA;                         //

  pinMode(13,OUTPUT);                    //
  pinMode(12,OUTPUT);                    //
  pinMode(11,OUTPUT);                    //

} //        >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<

void loop()
  unsigned long millisNow = millis();

  if (millisNow - TimeA >= TaskAtime) //Is it time to run Task A?
    TimeA = millis();                 //Re-initialize
  if (millisNow - TimeB >= TaskBtime) //Is it time to run Task B?
    TimeB = millis();                 //Re-initialize
  if (millisNow - TimeC >= TaskCtime) //Is it time to run Task C?
    TimeC = millis();                 //Re-initialize

  //Other stuff goes here

} //        >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<


void TaskA()
  digitalWrite(13,!digitalRead(13));   //Toggle pin 13
  //Other stuff


void TaskB()
  digitalWrite(12,!digitalRead(12));   //Toggle pin 12
  //Other stuff


void TaskC()
  digitalWrite(11,!digitalRead(11));   //Toggle pin 11
  //Other stuff


Thank you to every single one of you, really appreciated!

Oh well if we're showing code, here's some.

class myLED {
    byte _pin;
    boolean _state;
  public :
    // nothing happens in constructor
    myLED() {};
    // begin() called during setup();
    void begin(byte pin)
      _pin = pin;
      _state = false;
      pinMode(_pin, OUTPUT);
      digitalWrite(_pin, LOW);
    // run() called each loop();
    void run(unsigned long onMillis, unsigned long offMillis)
      static unsigned long startMillis = 0;
      unsigned long currentMillis = millis();
      if (currentMillis - startMillis >= (_state ? onMillis : offMillis))
        startMillis = currentMillis;
        _state = !_state;
        digitalWrite(_pin, (_state ? HIGH : LOW));

As a learning exercise I spent about a week of evenings blinking LEDs. I ended up with a lot of LED blinking code. This is just a class. Using it in a sketch I'd leave up to you.