Blinking two LEDs using timer

Hi guys,

I’m trying to blink two LEDs dependent of each other. Once one is blinking, the other turns off and waits for it to finish blinking then the other starts blinking and they keep doing this in an infinitely. Like a flip flop.
I have two variables called :

int stud= 0; //determine as many blinks as you want
int wheel=0; // determine as many blinks as you want

I want to put any number from 1 to 5 and adjust the blinking count. For example if I say :

int stud=5;
int wheel=3;

One LED blinks 5 times and after done, the other blinks 3 times.

I’m trying to use timers for the blink. I use the timer library from the following source:

I have written a code using this library but It doesn’t work. No matter what number I give, they only blink once each. Does anyone know what I’m missing?

#include "timer.h"

#define LED_PIN    12
#define LED_PORT()         pinMode(LED_PIN,OUTPUT)
#define LED_HIGH()         digitalWrite(LED_PIN,HIGH)
#define LED_LOW()          digitalWrite(LED_PIN,LOW)

#define LED_PIN1   7
#define LED_PORT1()         pinMode(LED_PIN1,OUTPUT)
#define LED_HIGH1()         digitalWrite(LED_PIN1,HIGH)
#define LED_LOW1()          digitalWrite(LED_PIN1,LOW)

#define LED_PIN2    6
#define LED_PORT2()         pinMode(LED_PIN2,OUTPUT)
#define LED_HIGH2()         digitalWrite(LED_PIN2,HIGH)
#define LED_LOW2()          digitalWrite(LED_PIN2,LOW)

bool led_is_on = false;  //Holds if the led is on
//Timer object
Timer timer5;
Timer timer6;

int stud=5;
int wheel=4;

//Set our awesome blink led function
void blinkLed1() {
  if(led_is_on) {
   LED_LOW();
  } 
 
  //Change our led state
 led_is_on = !led_is_on;
}

//Set our awesome blink led function
void blinkLed2() {
  if(led_is_on) {
   LED_LOW2();
  } 
  
  //Change our led state
 led_is_on = !led_is_on;
   }

void setup()
{
  LED_PORT();
  LED_PORT1();
  LED_PORT2();

  //Set the interval in milliseconds we want the led to blink
  timer5.setInterval(70,stud);
  timer6.setInterval(70,wheel);
  
  //Set our callback function
  timer5.setCallback(LED_Stud_Blink);
  timer6.setCallback(LED_Wheel_Blink);
  //Start the timer
  timer5.start();  //stud
  timer6.start();  //wheel
}

void loop()
{  
  //Update the timer
  timer5.update(); //stud
  timer6.update(); //wheel
}


void LED_Stud_Blink()
{
  if (stud==1){
    for (int i=0; i<1;i++){
      blinkLed1();
    }}
    
    if (stud==2){
    for (int i=0; i<2;i++){
      blinkLed1();
    }}
    
    if (stud==3){
    for (int i=0; i<3;i++){
      blinkLed1();
    }}
    
    if (stud==4){
    for (int i=0; i<4;i++){
      blinkLed1();
    }}
    
    if (stud==5){
    for (int i=0; i<5;i++){
      blinkLed1();
    }
  }
}
void LED_Wheel_Blink()
{
  if (wheel==1){
    for (int i=0; i<1;i++){
      blinkLed2();
    }}
    
    if (wheel==2){
    for (int i=0; i<2;i++){
      blinkLed2();
    }}
    
    if (wheel==3){
    for (int i=0; i<3;i++){
      blinkLed2();
    }}
    
    if (wheel==4){
    for (int i=0; i<4;i++){
      blinkLed2(); 
    }
    }
}

Do you really think that those defines makes it better to read ? For example the name “LED_PORT1” does not tell what it does.

What if you make just one software-millis-timer. That is the “framework” or “umbrella” and everything else is under that.
Inside that timer it is possible to blink a certain amount of times and choose which led is blinked.

Start by reading the Blink Without Delay page: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay.

The next code is not tested and without comment. It has probably bugs, but it shows the structure when a software-millis-timer is used.

unsigned long previousMillis;
unsigned long interval = 100;
const int studPin = 12;
const int wheelPin = 6;
int pin = studPin;
const int studCount = 20;
const int wheelCount = 25;
int count = studCount;
int led_state = LOW;

void setup()
{
  ...
}

void loop()
{
  unsigned long currentMillis = millis();

  if( currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis();

    if( led_state == LOW)
    {
      led_state = HIGH;
      digitalWrite( pin, led_state);
    }
    else
    {
      led_state = LOW;
      digitalWrite( pin, led_state);

      count++;
      if( pin == studPin)
      {
        if( count == studCount)
        {
          count = 0;
          pin = wheelPin;
        }
      }
      else
      {
        if( count == wheelCount)
        {
          count = 0;
          pin = studPin;
        }
      }
    }
  }
}

Koepel:
Do you really think that those defines makes it better to read ? For example the name "LED_PORT1" does not tell what it does.

What if you make just one software-millis-timer. That is the "framework" or "umbrella" and everything else is under that.
Inside that timer it is possible to blink a certain amount of times and choose which led is blinked.

Start by reading the Blink Without Delay page: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay.

The next code is not tested and without comment. It has probably bugs, but it shows the structure when a software-millis-timer is used.

unsigned long previousMillis;

unsigned long interval = 100;
const int studPin = 12;
const int wheelPin = 6;
int pin = studPin;
const int studCount = 20;
const int wheelCount = 25;
int count = studCount;
int led_state = LOW;

void setup()
{
 ...
}

void loop()
{
 unsigned long currentMillis = millis();

if( currentMillis - previousMillis >= interval)
 {
   previousMillis = currentMillis();

if( led_state == LOW)
   {
     led_state = HIGH;
     digitalWrite( pin, led_state);
   }
   else
   {
     led_state = LOW;
     digitalWrite( pin, led_state);

count++;
     if( pin == studPin)
     {
       if( count == studCount)
       {
         count = 0;
         pin = wheelPin;
       }
     }
     else
     {
       if( count == wheelCount)
       {
         count = 0;
         pin = studPin;
       }
     }
   }
 }
}

Thanks, I ran your code, one LED blinks infinitely while the other is always off

You don't have to quote my whole post, what I wrote is already there :wink:
I wrote: "It has probably bugs", so I assumed that it has more than one bug :o

Can you tell how you want to continue ? With a library or with code using millis() ?
Do you want us to present a working sketch that you don't understand ?
Which led is connected to which pin and do you have resistors with those leds ?
How fast should the leds blink ? Should one led blink with a different speed than the other ? Should the 'on' time of the leds be different than the 'off' time ?
Do you want to change the number of times they blink, while they are blinking ?

avoid repetitive code
consider

enum  { OFF = HIGH, ON = LOW, };

byte ledPin [] = { 10, 11, 12 };
#define N       sizeof(ledPin)

// -----------------------------------------------------------------------------
void setup(){
    Serial.begin(9600);
    for (unsigned n = 0; n < N; n++)  {
        digitalWrite (ledPin [n], OFF);
        pinMode (ledPin [n], OUTPUT);
    }
}

// -----------------------------------------------------------------------------
unsigned long interval [N] = { 300, 500, 700 };
unsigned      blinks   [N] = { 5, 3, 2 };

unsigned idx    = 0;
unsigned cnt    = 0;

unsigned long msecLst = 0;

void loop(){
    unsigned long msec = millis ();

    if ((msec - msecLst) > interval [idx])  {
        msecLst = msec;

        // toggle led
        byte state = digitalRead (ledPin [idx]);
        digitalWrite (ledPin [idx], ! state);

        // update cnt and idx
        if (ON == state)  {
            cnt++;
            if (blinks [idx] <= cnt)  {
                cnt = 0;
                idx++;
                if (N <= idx)  {
                    idx = 0;
                }
            }
        }
    }
}

@gcjr

Thank you very much, It works really well and the code looks awesome.
Added Karma. :slight_smile:

gcjr:
avoid repetitive code
consider

You know, your code works really well in a separate file but doesn't work when I merge it with my main code.
I have 4 loops for stud (variable) and 5 loops for Wheel (variable). When the system receives a message, it will go through one of these loops and they define which LED blinks and how many times.
I'll attach my code to my next post since it's very long.

#include <arduino.h>
#include "HopeDuino_LoRa.h"
#include <avr/sleep.h>
#include "ArduinoUniqueID.h"
#include <EEPROM.h>
#include "PCF85063A.h"
#include <avr/wdt.h>
#include "timer.h"
#include "timerManager.h"

/*#define LED_PIN    7
#define LED_PORT()         pinMode(LED_PIN,OUTPUT)
#define LED_HIGH()         digitalWrite(LED_PIN,HIGH)
#define LED_LOW()          digitalWrite(LED_PIN,LOW)

#define LED_PIN1  12
#define LED_PORT1()         pinMode(LED_PIN1,OUTPUT)
#define LED_HIGH1()         digitalWrite(LED_PIN1,HIGH)
#define LED_LOW1()          digitalWrite(LED_PIN1,LOW)

#define LED_PIN2    6
#define LED_PORT2()         pinMode(LED_PIN2,OUTPUT)
#define LED_HIGH2()         digitalWrite(LED_PIN2,HIGH)
#define LED_LOW2()          digitalWrite(LED_PIN2,LOW)*/


enum  { OFF = HIGH, ON = LOW, };
byte ledPin [] = { 23, 22};
#define N       sizeof(ledPin)
int stud;
int wheel;

void setup(void)
{
/*  LED_PORT();
 LED_PORT1();
 LED_PORT2();*/
 
   for (unsigned n = 0; n < N; n++)  {
       digitalWrite (ledPin [n], OFF);
       pinMode (ledPin [n], OUTPUT);
   }


Serial.begin(115200);
//while (!Serial) {;}
Serial.println("Device Initializing...");
}

   
unsigned long interval [N] = { 70, 70};
unsigned      blinks   [N] = { stud, wheel};
unsigned idx    = 0;
unsigned cnt    = 0;
unsigned long msecLst = 0;


void loop(void)
{

     unsigned long msec = millis ();

   if ((msec - msecLst) > interval [idx])  {
       msecLst = msec;

       // toggle led
       byte state = digitalRead (ledPin [idx]);
        if ((stud || wheel) !=0){
       // toggle led      
       digitalWrite (ledPin [idx], ! state);
         
       // update cnt and idx
       if (ON == state)  {
           cnt++;
           if (blinks [idx] <= cnt)  {
               cnt = 0;
               idx++;
               if (N <= idx)  {
                   idx = 0;
               }
           }
       }
   }
 
 }

 /**************************
       WHeel Number 
  *************************/

   if(EEPROM[0]==getstr[0] && EEPROM[1]==getstr[1] && EEPROM[2]==getstr[2] && EEPROM[3]==getstr[3]
   && EEPROM[4]==getstr[4] && EEPROM[5]==getstr[5] && EEPROM[6]==getstr[6] && EEPROM[7]==getstr[7]){
    writeStringToEEPROM(40, "WHEEL 1");
    int mem_counter=0;
// if (counter<1){
   wheel=1;
      Serial.println("XXXXXXXXX Slave 1 XXXXXXXXXX");
      timer1.reset();
     }
     Serial.println("XXXXXXXXX Slave 1 is alive XXXXXXXXXX");
          }
     
    if(EEPROM[9]==getstr[0] && EEPROM[10]==getstr[1] && EEPROM[11]==getstr[2] && EEPROM[12]==getstr[3]
    && EEPROM[13]==getstr[4] && EEPROM[14]==getstr[5] && EEPROM[15]==getstr[6] && EEPROM[16]==getstr[7]){
     writeStringToEEPROM(79, "WHEEL 2");
     int mem_counter=0;
  if (counter<1){
      counter++;    
      Serial.println("********** Slave 2 **********");
    wheel=2;


      timer2.reset();
      }
     // Serial.println("XXXXXXXXX Slave 2 is alive XXXXXXXXXX");
      }
     
     if(EEPROM[17]==getstr[0] && EEPROM[18]==getstr[1] && EEPROM[19]==getstr[2] && EEPROM[20]==getstr[3]
&& EEPROM[21]==getstr[4] && EEPROM[22]==getstr[5] && EEPROM[23]==getstr[6] && EEPROM[24]==getstr[7]){
 writeStringToEEPROM(117, "WHEEL 3");
 int mem_counter=0;
 if (counter<1){  
      Serial.println("XXXXXXXXX Slave 3 XXXXXXXXXXX");
   wheel= 3;
      timer3.reset();
     }

     // Serial.println("XXXXXXXXX Slave 3 is alive XXXXXXXXXX");
      }
     
     if(EEPROM[25]==getstr[0] && EEPROM[26]==getstr[1] && EEPROM[27]==getstr[2] && EEPROM[28]==getstr[3]
&& EEPROM[29]==getstr[4] && EEPROM[30]==getstr[5] && EEPROM[31]==getstr[6] && EEPROM[32]==getstr[7]){
 writeStringToEEPROM(156, "WHEEL 4");
 int mem_counter=0;
 if (counter<1){
   
      Serial.println("XXXXXXXXX Slave 4 XXXXXXXXXXX");
      wheel=4;
     
      timer4.reset();
     }  
    //  Serial.println("XXXXXXXXX Slave 4 is alive XXXXXXXXXX");
      }    

  if (getstr[0]== str0[0] && getstr[1]== str0[1]&& getstr[2]== str0[2]&& getstr[3]== str0[3]&& getstr[4]== str0[4] && getstr[5]== str0[5]){
      //if (counter_stud<1){
      stud=1;
      Serial.println("Stud 1 is loose");
      writeStringToEEPROM(196, "STUD 1");
     
      }
   
     if (getstr[0]== str1[0] && getstr[1]== str1[1]&& getstr[2]== str1[2]&& getstr[3]== str1[3]&& getstr[4]== str1[4] && getstr[5]== str1[5]){    
      Serial.println("Stud 2 is loose");
      writeStringToEEPROM(206, "STUD 2");
       stud=2;
       
      }
   
     if (getstr[0]== str2[0] && getstr[1]== str2[1]&& getstr[2]== str2[2]&& getstr[3]== str2[3]&& getstr[4]== str2[4] && getstr[5]== str2[5]){    
      Serial.println("Stud 3 is loose");
      writeStringToEEPROM(216, "STUD 3");
      stud=3;
       
      }
     
     if (getstr[0]== str3[0] && getstr[1]== str3[1]&& getstr[2]== str3[2]&& getstr[3]== str3[3]&& getstr[4]== str3[4] && getstr[5]== str3[5]){      
      Serial.println("Stud 4 is loose");
      writeStringToEEPROM(226, "STUD 4");
      stud=4;
       
      }
   
    if (getstr[0]== str4[0] && getstr[1]== str4[1]&& getstr[2]== str4[2]&& getstr[3]== str4[3]&& getstr[4]== str4[4] && getstr[5]== str4[5]){  
      Serial.println("Stud 5 is loose");
      writeStringToEEPROM(236, "STUD 5");
      stud=5;
      }  
 }  
      else{
    /*  LED_HIGH();
      LED_HIGH1();
      LED_HIGH2();*/
   }
 
}

This is a trimmed part of the code. There’s a section called “Wheel Number” and there are some IF loops afterwards. I want to set the number of blinks individually when the program enters that loop.
For example, I want the Stud number and Wheel number to be set to zero at first which means LEDs are ON without blinking. When I get a Wheel message, the corresponding LED blinks the number in that loop for WHEEL No (for example Wheel=5; means blink that LED 5 times infinitely). Then if it receives a Stud message, the corresponding LED blinks the number in that loop for STUD No.
If one is received, the corresponding LED blinks while the other one is turned OFF to receive it’s message and when it does, One blinks the number defined in that loop and the the other one waits, then it blinks its own number while the first one waits and this happens infinitely.
I also want the LED_PORT and LED_HIGH functions to work as well if possible.
I know it’s such an easy code but my head is blowing up, I’m just a beginner.

Press Ctrl+T in the Arduino IDE.

You have this:

void loop(void)
{
  ...
  if (EEPROM[0] ...
}

if (EEPROM[9] ...

Koepel:
Press Ctrl+T in the Arduino IDE.

Thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.