Doubs about blink without delay in several leds

Good afternoon im new in this forum im glad to meet you :smiley:

My problem its that i dont get how to blink several leds acording some ifs. Lets say that i have got a house and i want that leds blink depends of the temperature, i got 1 led that Will blink if the temperature in both rooms reach a value. Also got 1 led in each rooms that blink at 20ºC.
If decrease 20ºc Will blink at 2 seconds and if increase Will blink at 5 seconds.
Sorry if im explaining my issue wrong, English is not my first lenguage.

My code for the moment its the following one:

int Led11 = 2;
int Led12 = 3;
int LedF1 = 4;






float Conversion(char Channel, float VoltageMinus, float VoltagePlus, float RangeMinus, float RangePlus)
{

  float Sensor = analogRead(Channel);
  return (Sensor - VoltageMinus)*(RangePlus-RangeMinus)/(VoltagePlus - VoltageMinus) + RangeMinus;
  
}





void Blink(unsigned long Rate, int LED)
{
  static unsigned long MillisBefore = 0;
  static int EstateLed = LOW;
  unsigned long MillisNow = millis();
 
  if(MillisNow - MillisBefore >= Rate)
  {
    MillisBefore = MillisNow;
    if(EstateLed == LOW)
    {
      EstateLed = HIGH;
    }
    else
    {
      EstateLed = LOW;
    }
    digitalWrite(LED,EstateLed);
  }
  
 
}



void General()

{

  if((Conversion(A0,0.0,1023.0,-10.0,70.0) + Conversion(A1,0.0,1023.0,-10.0,70.0)) / 2.0 <= 20.0)
  {
    
    Blink(LedF1,2000);
      if (Conversion(A0,0.0,1023.0,-10.0,70.0) <= 20.0)
      {
        Blink(Led11,2000);
      }
      else if(Conversion(A0,0.0,1023.0,-10.0,70.0) > 20.0)
      {
        Blink(Led11,5000);
      }
      else if(Conversion(A1,0.0,1023.0,-10.0,70.0) <= 20.0)
      {
        Blink(Led12,2000);
      }
      else if(Conversion(A1,0.0,1023.0,-10.0,70.0) > 20.0)
      {
        Blink(Led12,5000);
      }
  }
  
  else
  {
    Blink(LedF1,5000);
  }
    
}


void setup() {

Serial.begin(9600);
pinMode(Led11,OUTPUT);
pinMode(Led12,OUTPUT);
pinMode(LedF1,OUTPUT);
}





void loop() 
{
General();
}

Thanks for your time :smiley:

void Blink(unsigned long Rate, int LED)  // function definition, note: rate, led
   Blink(LedF1,2000);  // function call, note: led, rate

The arguments are not consistent.

You’ve got the problem that all the LEDs will share a single “MillisBefore” variable.

Different ways around this include:

  1. A “blink” class
  2. Pass the blink function a structure which describes the pin, the rate, and the “MillisBefore” for that LED.

You have 2 issues:

1) you have your arguments reversed in the Blink call 2) you need separate millisecond variables and led state variables for each LED

Here is an example of using a ‘struct’ to bundle together the LED pin number, MillisBefore, LED state, and blink rate. That way they can travel together and be passed to a single “Blink()” function.

Note the use of the ‘ternary’ operator: ‘?’. The statement “X = A ? B : C;” means “if A is ‘true’, set X to B, else set X to C”. It is a handy shortcut for:

  if (A)
    X = B;
  else
    X = C;
const int Led11Pin = 2;
const int Led12Pin = 3;
const int LedF1Pin = 4;


struct Blinker
{
  int LedPin;
  unsigned long MillisBefore;
  boolean LedOn;
  unsigned long Rate;
};


Blinker Blink11 = {Led11Pin, 0, false, 1000};
Blinker Blink12 = {Led12Pin, 0, false, 1000};
Blinker BlinkF1 = {LedF1Pin, 0, false, 1000};


float Conversion(char Channel, float VoltageMinus, float VoltagePlus, float RangeMinus, float RangePlus)
{
  float Sensor = analogRead(Channel);
  return (Sensor - VoltageMinus) * (RangePlus - RangeMinus) / (VoltagePlus - VoltageMinus) + RangeMinus;
}


void Blink(Blinker b)
{
  unsigned long MillisNow = millis();


  if (MillisNow - b.MillisBefore >= b.Rate)
  {
    b.MillisBefore = MillisNow;
    b.LedOn = !b.LedOn;
    digitalWrite(b.LedPin, b.LedOn ? HIGH : LOW);
  }
}


void General()
{
  float t1 = Conversion(A0, 0.0, 1023.0, -10.0, 70.0);
  float t2 = Conversion(A1, 0.0, 1023.0, -10.0, 70.0);
  float tAverage = (t1 + t2) / 2;


  if (tAverage <= 20.0)
  {
    BlinkF1.Rate = 2000;


    Blink11.Rate = (t1 <= 20.0) ? 2000 : 5000;
    Blink(Blink11);


    Blink12.Rate = (t2 <= 20.0) ? 2000 : 5000;
    Blink(Blink12);
  }
  else
  {
    BlinkF1.Rate = 5000;
  }
  
  Blink(BlinkF1);
}

void setup()
{
  Serial.begin(9600);
  pinMode(Led11Pin, OUTPUT);
  pinMode(Led12Pin, OUTPUT);
  pinMode(LedF1Pin, OUTPUT);
}


void loop()
{
  General();
}

Hi guys. I see my mistakes and now im trying to make this by a struct.

Im trying to imitate the johnwasser´s code. But i have got an issue about this. I think that i understand the code but when i run it my leds just turn on and dont blink.

I dont really see any wrong in the code…
Can someone tell me why the leds dont blink?

Thanks for your time :smiley:

This is the updated code:

int Led11 = 2;
int Led12 = 3;
int LedF1 = 4;




typedef struct Blinker
{
  int LedPin;
  unsigned long MillisBefore;
  bool State;
  unsigned long Rate;
};

Blinker Blink11 = {Led11,0,false,1000};
Blinker Blink12 = {Led12,0,false,1000};
Blinker BlinkF1 = {LedF1,0,false,1000};






float Conversion(char Channel, float VoltageMinus, float VoltagePlus, float RangeMinus, float RangePlus)
{

  float Sensor = analogRead(Channel);
  return (Sensor - VoltageMinus)*(RangePlus-RangeMinus)/(VoltagePlus - VoltageMinus) + RangeMinus;
  
}


void Blink(Blinker b)
{

  unsigned long MillisNow = millis();
 
  if(MillisNow - b.MillisBefore >= b.Rate)
  {
    b.MillisBefore = MillisNow;
    b.State = !b.State;
    digitalWrite(b.LedPin, b.State ? HIGH : LOW);
  } 
}





void General()
{

  float T11 = Conversion(A0,0.0,1023.0,-10.0,70.0);
  float T12 = Conversion(A1,0.0,1023.0,-10.0,70.0);
  float TF1 = (T11+T12)/2;

  if(TF1 <= 20.0)
  {
    BlinkF1.Rate = 2000;
    Blink11.Rate = (T11 <= 20.0) ? 2000 : 5000;
    Blink(Blink11);

    Blink12.Rate = (T12 <= 20.0) ? 2000 : 5000;
    Blink(Blink12);
  }
  else
  {
    BlinkF1.Rate = 5000;
  }
  Blink(BlinkF1);
}








void setup() {


Serial.begin(9600);
pinMode(Led11,OUTPUT);
pinMode(Led12,OUTPUT);
pinMode(LedF1,OUTPUT);




void loop() {
 
 
General();



}

You must pass a reference to the Blinker struct, not the struct itself.

Otherwise the "b" within function "Blink()" is just a local copy. Any changes you make to it will be lost at the end of the function.

Edit:

void Blink(Blinker &b)