RTC Widget not turning on leds

Hello I am working on a project and can`t figure out my problems. I have an led strip that I need to turn on slowly in the morning every 15 minutes in small steps and turn off every 15 minutes. But I cant figure out why some times it does not work porperly but the time is displayed correct.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
//#define button V1
#define red_pin 13
#define blue_pin 14
#define green_pin 12

int st1 = 10;
int st2 = 100;
int st3 = 500;
int st4 = 800;
int st5 = 1024;




// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icst).
char auth[] = "dc51exxxxxxxxxxxxxxxxxxxxxxxxedbcb";

SimpleTimer timer;

WidgetRTC rtc;

BLYNK_ATTACH_WIDGET(rtc, V5);


// Digital clock display of the time
void clockDisplay()
{

  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
            hour(), minute(), second(),
            day(), month(), year());
}

void turnon()
{

  if (hour() == 7, minute() == 00)
  {

    analogWrite(red_pin, st1);
    analogWrite(blue_pin, st1);
    analogWrite(green_pin, st1);
  }



  if (hour() == 7, minute() == 15)
  {
    analogWrite(red_pin, st2);
    analogWrite(blue_pin, st2);
    analogWrite(green_pin, st2);
  }


  if (hour() == 7, minute() == 30)
  {
    analogWrite(red_pin, st3);
    analogWrite(blue_pin, st3);
    analogWrite(green_pin, st3);
  }





  if (hour() == 7, minute() == 45)
  {
    analogWrite(red_pin, st4);
    analogWrite(blue_pin, st4);
    analogWrite(green_pin, st4);
  }

  if (hour() == 8, minute() == 00)
  {

    analogWrite(red_pin, st5);
    analogWrite(blue_pin, st5);
    analogWrite(green_pin, st5);
  }


  if (hour() == 21, minute() == 00)
  {
    analogWrite(red_pin, st4);
    analogWrite(blue_pin, st4);
    analogWrite(green_pin, st4);
  }


  if (hour() == 21, minute() == 15)
  {
    analogWrite(red_pin, st3);
    analogWrite(blue_pin, st3);
    analogWrite(green_pin, st3);
  }



  if (hour() == 21, minute() == 30)
  {
    analogWrite(red_pin, st2);
    analogWrite(blue_pin, st2);
    analogWrite(green_pin, st2);
  }


  if (hour() == 21, minute() == 45)
  {

    analogWrite(red_pin, st1);
    analogWrite(blue_pin, st1);
    analogWrite(green_pin, st1);
  }


  if (hour() == 22, minute() == 00)
  {
    analogWrite(red_pin, 0);
    analogWrite(blue_pin, 0);
    analogWrite(green_pin, 0);
  }


  if (hour() == 22, minute() == 15)
  {
    analogWrite(red_pin, 0);
    analogWrite(blue_pin, 0);
    analogWrite(green_pin, 0);
  }


}



void setup()
{
  Serial.begin(9600); // See the cstnectist status in Serial Mstitor
  Blynk.begin(auth, "Claxxxxxxxxxxxxxxxx", "clxxxxxx13");

  while (Blynk.connect() == false) {
    // Wait until connected
  }


  pinMode(red_pin, OUTPUT);
  pinMode(blue_pin, OUTPUT);
  pinMode(green_pin, OUTPUT);


  // Begin synchronizing time
  rtc.begin();

  // Display digital clock every 1 sec

  timer.setInterval(10000L, turnon);
  timer.setInterval(1000L, clockDisplay);

}

void loop()
{
  Blynk.run();
  timer.run();
}

Did you mean:

if (hour() == 8 && minute() == 00)

Note “&&” is logical And. The comma operator “,” is almost never what you want in c++ (except to pass arguments to a function).

Since I’m here your program might be more robust if you write it with “if-else” statements like so:

  int value = 0;
  if (hour() >= 7 && minute() >= 00)
  {
      value = st1;
  }
  
  else if (hour() >= 7 && minute() >= 15)
  {
      value = st2;
  }
  
  //...etc...

  analogWrite(red_pin, value);
  analogWrite(blue_pin, value);
  analogWrite(green_pin, value);

First off this removes a lot of the tedious repetition. Secondly if you program is cut off from the network for a minute and the clock went from (say) 7 14 to 7 16 your code would still work - or more usefully, if you switch it on at 7:16 it will achieve the correct brightness straight away.

I tried what you suggested but at 13:40 the leds where at low brightness they are suppose to be at full.


void turnon()
{
  int value = 0;
  if (hour() >= 7  && minute() >= 00)
  {

    value = st1;
  }



  else  if (hour() >= 7  && minute() >= 15)
  {
    value = st2;
  }


  else if (hour() >= 7 && minute() >= 19)
  {
    value = 1023;
  }





  else if (hour() >= 7 && minute() >= 45)
  {
    value = st4;
  }

  else if (hour() >= 8 && minute() >= 00)
  {

    value = st5;
  }


  else if (hour() >= 21 && minute() >= 00)
  {
    value = st4;
  }


  else if (hour() >= 21 && minute() >= 15)
  {
    value = st3;
  }



  else if (hour() >= 21 && minute() >= 30)
  {
    value = st2;
  }


  else  if (hour() >= 21 && minute() >= 45)
  {

    value = st1;
  }


  else  if (hour() >= 22 && minute() >= 00)
  {
    value = 0;
  }

 
    analogWrite(red_pin, value);
    analogWrite(blue_pin, value);
    analogWrite(green_pin, value);

}

Have you got working debug? How about you add this:

BLYNK_LOG("Time: %d %d - setting lights to %d", hour(), minute(), value);
analogWrite(red_pin, value);
analogWrite(blue_pin, value);
analogWrite(green_pin, value);

Also realised by logic is flawed, needs to be:

if (hour() >= 7  && minute() >= 00 && minute() < 15) {
   ...
} 
else if (hour() >= 7  && minute() >= 15 && minute() < 30) {
   ...
}

My bad, sorry :flushed:

It’s quite easy to make stupid mistake like this. So how about doing this instead.:

 int value = 0;

 int msm = hour() * 60 + minute(); //minutes since midnight

 if(msm < 7*60+0) {
    value = 0
 }

 else if(msm < 7*60+15) {
    value = st1
 }

 ....

As a bonus you could even make this smooth and continuous using map:

#define SUNRISE 7*60 + 0
#define DAWN 8*60 + 0
#define DUSK 21*60 + 0
#define SUNSET 22*60 + 0

...

void turn_on() {
    int value = 0;
    int msm = hour() * 60 + minute(); //minutes since midnight

    if ( msm < SUNRISE ) {
         value = 0;
    } else if(msm < DAWN) {
         value = map(msm, SUNRISE, DAWN, 0, 1024);
    } else if(msm < DUSK ) {
         value = 1024;
    } else if(msm < SUNSET ) {
        value = map(msm, DUSK, SUNSET, 1024, 0);
    } else { 
        value = 0;
    }
    
    ...
}

Dont worry everyone makes mistakes. Ok so it will delete the void turnon() in my code and replace it with your part. Is that right and will the light incease over time?

Thanks for taking the time to help me.

It might work. This is all off the top of my head - no guarantees. SAVE THE VERSION YOU WROTE FIRST!

I suggest you add the debug like too:

BLYNK_LOG("Time: %d %d - setting lights to %d", hour(), minute(), value);

If you’re not sure what’s going on then put in more debug!

I think some thing is wrong when I upload the code in arduino I get

C:\Users\Hayley-PC\Downloads\files (5)\Aquarium\Aquarium.ino: In function 'void turn_on()':

Aquarium:78: error: too few arguments to function 'long int map(long int, long int, long int, long int, long int)'

          value = map(SUNRISE, DAWN, 0, 1024);

                                            ^

In file included from sketch\Aquarium.ino.cpp:1:0:

C:\Users\Hayley-PC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\cores\esp8266/Arduino.h:275:6: note: declared here

 long map(long, long, long, long, long);

      ^

Aquarium:82: error: too few arguments to function 'long int map(long int, long int, long int, long int, long int)'

         value = map(DUSK, SUNSET, 1024, 0);

                                          ^

In file included from sketch\Aquarium.ino.cpp:1:0:

C:\Users\Hayley-PC\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.2.0\cores\esp8266/Arduino.h:275:6: note: declared here

 long map(long, long, long, long, long);

      ^

exit status 1
too few arguments to function 'long int map(long int, long int, long int, long int, long int)'

You’re right, you need msm in there as the first parameter. I fixed the code above ^^

1 Like

It finally worked I could not have done it with out you. I don`t know much about coding with arduino but you sure tough me. One last thing if I want to change the colours of the leds from the blynk app what can I do?

1 Like

I added a Ds18B20 temp sensor so if the temperature rises or lowers it will notify me but I can get the notifications and I am getting the temperature only in the blynk app. What can I change for the code to work?

 #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
    #include <SPI.h>
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>
    #include <TimeLib.h>
    #include <WidgetRTC.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #define red_pin 13
    #define blue_pin 14
    #define green_pin 12
    #define SUNRISE 7*60 + 0
    #define DAWN 8*60 + 0
    #define DUSK 21*60 + 0
    #define SUNSET 22*60 + 0
    #define ONE_WIRE_BUS 5
    #define Pin 5//  pin DATA ds18b20
    

    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icst).
    char auth[] = "dc51xxxxxxxxxxxxxxbcb";

    SimpleTimer timer;

    WidgetLED ledred(V1);
    WidgetLED ledgreen(V2);

    OneWire ourWire(Pin);
    DallasTemperature sensors(&ourWire);

    WidgetRTC rtc;

    BLYNK_ATTACH_WIDGET(rtc, V5);


    // Digital clock display of the time
    void clockDisplay()
    {

      // You can call hour(), minute(), ... at any time
      // Please see Time library examples for details
      BLYNK_LOG("Current time: %02d:%02d:%02d %02d %02d %d",
                hour(), minute(), second(),
                day(), month(), year());
    }



    void turn_on() {
      int value = 0;
      int msm = hour() * 60 + minute(); //minutes since midnight

      if ( msm < SUNRISE ) {
        value = 0;
      } else if (msm < DAWN) {
        value = map(msm, SUNRISE, DAWN, 0, 1024);
      } else if (msm < DUSK ) {
        value = 1024;
      } else if (msm < SUNSET ) {
        value = map(msm, DUSK, SUNSET, 1024, 0);
      } else {
        value = 0;
      }
      BLYNK_LOG("Time: %d %d - setting lights to %d", hour(), minute(), value);
      analogWrite(red_pin, value);
      analogWrite(blue_pin, value);
      analogWrite(green_pin, value);
    }



    void sendTemps()
    {
      sensors.requestTemperatures();
      Blynk.virtualWrite(0, sensors.getTempCByIndex(0));
      BLYNK_LOG("Temperature %2d ", sensors);

      if (sensors >= 29)

      {
        Blynk.notify("High Temperature");
        ledred.on();
        ledgreen.off();
      }
      else  if (sensors <= 26)
      {
        Blynk.notify("Low Temperature");
        ledred.on();
        ledgreen.off();
      }

    }
    void setup()
    {
      Serial.begin(9600); // See the cstnectist status in Serial Mstitor
      Blynk.begin(auth, "CxxxxxxxxxxxFi", "cxxxxxxxxxxx3");


      while (Blynk.connect() == false) {
        // Wait until connected
      }


      pinMode(red_pin, OUTPUT);
      pinMode(blue_pin, OUTPUT);
      pinMode(green_pin, OUTPUT);


      // Begin synchronizing time
      rtc.begin();
      sensors.begin();

      // Display digital clock every 1 sec

      timer.setInterval(10000L, turn_on);
      timer.setInterval(1000L, clockDisplay);
      timer.setInterval(5000L, sendTemps); // Temperature sensor polling interval (5000L = 5 seconds)

    }




    void loop()
    {
      Blynk.run();
      timer.run();
    }


What should I write instead of ???

      void sendTemps()
        {
          sensors.requestTemperatures();
          Blynk.virtualWrite(0, sensors.getTempCByIndex(0));
          BLYNK_LOG("Temperature %2d ",???????);

          if (????????>= 29)

          {
            Blynk.notify("High Temperature");
            ledred.on();
            ledgreen.off();
          }
          else  if (??????? <= 26)
          {
            Blynk.notify("Low Temperature");
            ledred.on();
            ledgreen.off();
          }

        }