# Making timer with RTC clock

**URL:** <https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807>\
**Category:** Programming\
**Created:** [June 23, 2022, 11:57am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807 "2022-06-23T11:57:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 23, 2022, 11:57am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/1 "2022-06-23T11:57:41Z")

</div>

Hello

I am trying to make a scheduler with an esp8266 using RTC time and the TimeLib.h library

I chose for the tests to run on a cycle where 1 hour = 1 second

I can't catch the seconds. Could someone explain to me how to do it?  
And possibly tell me if my method is good and if not how to improve my code.

Thanks

```cpp
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "31k3lWUXbqt4bbcn4bROU1vI6d42x1C2";
char ssid[] = "xx";
char pass[] = "xx";

BlynkTimer timer;

WidgetRTC rtc;

int temps = second();

BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(10, 3, 141, 1), 8080);

  setSyncInterval(800);
  

  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, cycle1);
}

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

void clockDisplay()
{

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " / " + month() + " / " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

void cycle1()
{
  if (temps == 7)
  {
    Serial.println("debut de cylcle");
    digitalWrite (15, LOW);
    Serial.println ("lampe on");
    delay(18000);
    Serial.println ("fin de cycle");
    digitalWrite(15, HIGH);
    Serial.print ("lampe off");
  }
  else{
}
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 23, 2022, 12:04pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/2 "2022-06-23T12:04:03Z")

</div>

> [@CharlesTestas](#):
>
> I can't catch the seconds.

Do you mean that

```arduino
  if (temps == 7)

```

never returns true ?

If so, where in the code is the value of temps ever changed ? Have you tried printing the value of temps before testing it ?

If that is not the problem, then please describe the problem in more detail

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 23, 2022, 12:05pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/3 "2022-06-23T12:05:55Z")

</div>

You are not only making a timer. You are using Blynk.  
If I remember right blynk doesn't like traffic at short intervalls.  
updating via blinkserver once per second is a to high frequency.  
imagine each and every of the hundred-thousands of Blynk-users are doing this. That would be almost a DdoS-attack.

That's the reason why Blynk is limiting this

Is blynk a substantial requirement of your project or can you throw it out?

best regards Stefan

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [June 23, 2022, 12:10pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/4 "2022-06-23T12:10:15Z")

</div>

This will block the code, and the clock update, for 18 seconds: `delay(18000)` = Ooops..

EDIT: And this `int temps = second()` is run before the clock has been initialized, you should check what value you get there - if it is 7, the code will block for 18 seconds in each iteration of `loop()`...

---

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 23, 2022, 12:33pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/6 "2022-06-23T12:33:53Z")

</div>

> [@Danois90](#):
>
> EDIT: And this `int temps = second()` is run before the clock has been initialized, you should check what value you get there - if it is 7

I actually don't have a value, so I placed my function `int time = seconds();` after the` rtc.begin();` but I still have no value. Maybe my timing is wrong.

> [@Danois90](#):
>
> the code will block for 18 seconds in each iteration of `loop()`...

But the only other solution I see is to call a cycle end time (in this case when second equals 25) I will work with hour but seconds is better for testing

> [@StefanL38](#):
>
> If I remember right blynk doesn't like traffic at short intervalls.  
> updating via blinkserver once per second is a to high frequency.

The problem is not there, I have a dozen sensors that send data every 0.5 seconds and everything works  
I also specify that I am on a local server

> [@UKHeliBob](#):
>
> Do you mean that
> 
> ```arduino
> if (temps == 7)
> 
> ```
> 
> never returns true ?
> 
> If so, where in the code is the value of temps ever changed ? Have you tried printing the value of temps before testing it ?

That's i mean

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 23, 2022, 1:13pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/7 "2022-06-23T13:13:23Z")

</div>

You need to update the value of temps frequently, each time through loop() would be good, not use blocking code anywhere in the sketch, and test its value as frequently as you update its value

---

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 23, 2022, 1:17pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/8 "2022-06-23T13:17:28Z")

</div>

I don't know how i can do it...

Except

```cpp
void cycle1()
{
  if (temps == 7)
  {
    Serial.println("debut de cylcle");
    digitalWrite (15, LOW);
    Serial.println ("lampe on");
if else (temps == 18)
{
    Serial.println ("fin de cycle");
    digitalWrite(15, HIGH);
    Serial.print ("lampe off");
  }
  else{
}
}

```

You think it good like that ?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 23, 2022, 1:31pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/9 "2022-06-23T13:31:54Z")

</div>

what does

> [@CharlesTestas](#):
>
> ```arduino
> if (temps == 7)
> ...
> if else (temps == 18)
> 
> ```

mean?  
temps like time?  
and if it is time what kind of time?  
seconds?, minutes?, hours?, daily hour?

I did not expect that you use things like delay(18000) in your code if you want to have an RTC-clock.

There is a **very different** approach for timing. **Non** -blocking timing  
It is so completely different that you have to think **really new**.

As long as you try to think of this non-blocking timing it is a delay-similar thing  
you will block yourself and it is just confusing. Because it is **really different**.

Here is a tutorial that explains the basic principle:

> [@Example-code for timing based on millis() easier to understand through the use of example-numbers / avoiding delay()](https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017):
>
> non-blocking timing. Execute code only from time to time. There are a lot of different ways to learn programming. This thread wants to add another approach that is different to the yet existing ones. UPDATE 06.01.2023 if you are mainly interested in applying non-blocking timing you can do a quick read of this short tutorial / demonstration If you are intersted in understanding the details how it works go on reading here. I use an everyday analogon in this post to explain the basic principle …

best regards Stefan

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 23, 2022, 1:52pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/10 "2022-06-23T13:52:57Z")

</div>

> [@CharlesTestas](#):
>
> You think it good like that ?

No. As @StefanL38 has pointed out you have got the syntax if if/else if wrong.

I have asked before and got no answer. Where is the value of temps being updated in the sketch ?

You initialise it

```arduino
int temps = second();

```

but after that what changes it ?

---

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 23, 2022, 3:49pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/11 "2022-06-23T15:49:38Z")

</div>

> [@CharlesTestas](#):
>
> ```arduino
> if (temps == 7)
> ...
> if else (temps == 18)
> 
> ```

Sorry it's my bad, i'm tired !

> [@CharlesTestas](#):
>
> ```arduino
> if (temps == 7)
> ...
> if else (temps2 == 18)
> 
> ```

> [@UKHeliBob](#):
>
> You initialise it
> 
> ```arduino
> int temps = second();
> 
> ```
> 
> but after that what changes it ?

`If` is looking if temps == 7 no ?

> [@StefanL38](#):
>
> There is a **very different** approach for timing. **Non** -blocking timing  
> It is so completely different that you have to think **really new**.

What is the best approach in your opinion ?

RTC based or millis ?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 23, 2022, 4:44pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/12 "2022-06-23T16:44:38Z")

</div>

> [@CharlesTestas](#):
>
> `If` is looking if temps == 7 no ?

It can look to see whether temps equals 7 as long as you want but if you don't update the value of temps then how will it ever equal 7 ?

Suppose that you had an old fashioned wind up clock that was not running and you set it to a time of 1 o'clock. How long do you think it would be until the clock showed 7 o'clock ?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 23, 2022, 4:44pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/13 "2022-06-23T16:44:51Z")

</div>

> [@CharlesTestas](#):
>
> What is the best approach in your opinion ?
> 
> RTC based or millis ?

You are using an ESP8266. If this ESP8266 is connected to WiFi the onboard RTC will be updated by the WiFi-connection in the backround.  
RTC is more precise than function millis().

But your problem is at an

## other place

your basic code is

## unable to work for timing!

there is only

## one place

where you assign a value to variable "temps"  
it is this line

```arduino
int temps = second();

```

in all the rest of the code your variables "temps" gets

## never updated / never changed

how should this variable ever become

```arduino
  if (temps == 7)

```

## ????????????????????????????

best regards Stefan

---

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 23, 2022, 7:24pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/14 "2022-06-23T19:24:06Z")

</div>

> [@UKHeliBob](#):
>
> It can look to see whether temps equals 7 as long as you want but if you don't update the value of temps then how will it ever equal 7 ?
> 
> Suppose that you had an old fashioned wind up clock that was not running and you set it to a time of 1 o'clock. How long do you think it would be until the clock showed 7 o'clock ?

I try to understand but I have learning disabilities, it's not easy for me.

Can you give me an example?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [June 23, 2022, 7:27pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/15 "2022-06-23T19:27:53Z")

</div>

> [@CharlesTestas](#):
>
> Can you give me an example?

example of what?

It might be that you have difficulties to learn. But there is no way around precise information.

best regards Stefan

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 23, 2022, 7:33pm UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/16 "2022-06-23T19:33:21Z")

</div>

> [@CharlesTestas](#):
>
> Can you give me an example?

My question about the non working clock was an example of the problem with your code

In your sketch where does the time, particularly the seconds value, come from ?

---

<div class="post-metadata">

**Author:** ![CharlesTestas](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@CharlesTestas](https://forum.arduino.cc/u/CharlesTestas)\
**Post date:** [June 24, 2022, 9:28am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/17 "2022-06-24T09:28:57Z")

</div>

Ok now my programmer works, but I lost the RTC display. At launch all my displays are at zero and count the time from the ignition of the ESP

Sketch :

```cpp
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = "xxx";
char ssid[] = "xxxx";
char pass[] = "xxx";

BlynkTimer timer;

WidgetRTC rtc;

BLYNK_CONNECTED() {
  // Synchronize time on connection
  
}

int OnHours = 7;
int OffHours = 25;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, IPAddress(10, 3, 141, 1), 8080);
  rtc.begin();

  setSyncInterval(800);

  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, cycle1);
}

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

void clockDisplay()
{

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " / " + month() + " / " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

void cycle1()
{

  int Hours = second();
  if (Hours == OnHours)
  {
    Serial.print("debut de cylcle ");
    digitalWrite (15, HIGH);
    Serial.println ("lampe on");
  }

  if (Hours == OffHours)
  {
    Serial.print("fin de cylcle ");
    digitalWrite (15, LOW);
    Serial.println ("lampe off");
  }

}

```

EDIT : my bad i forgot RTC widget on Blynk

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [June 24, 2022, 9:38am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/18 "2022-06-24T09:38:14Z")

</div>

If the intent is to have the pin HIGH for 7 seconds/hours and LOW for 25, that is not what the code does. It will become HIGH whenever the second/hour is 7 and LOW again when it becomes 25 (hour will never become 25). If the goal is to have the pin HIGH for 18 seconds/hours and LOW for 25, you could use a one-shot timer to turn it LOW.

```cpp
#define PIN_NUM 15
#define TIME_HIGH_MS 18000UL

void cycle1()
{
  if ((second() == 7) && (digitalRead(PIN_NUM) == LOW))
  {
    digitalWrite(PIN_NUM, HIGH);
    timer.setTimeout(TIME_HIGH_MS, cycle2);
  }
}

void cycle2()
{
  digitalWrite(PIN_NUM, LOW);
}

```

Even though I am not familiar with the timer library, the example should do the job without blocking.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 24, 2022, 9:42am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/19 "2022-06-24T09:42:04Z")

</div>

```cpp
  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, cycle1);

```

I know very little about Blynk but shouldn't you have 2 timer objects if you want to call 2 separate functions at the end of the period ?

To me, those 2 lines of code say "run the clockDisplay function every second. No, sorry, I have changed my mind. Run the cycle1 function every second" In other words, never call the clockDisplay function

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [June 24, 2022, 10:09am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/20 "2022-06-24T10:09:35Z")

</div>

[BlynkTimer](https://docs.blynk.io/en/blynk.edgent-firmware-api/blynk-timer) is a subclass to [SimpleTimer](https://playground.arduino.cc/Code/SimpleTimer/) which allows multiple timer slots. So no, you do not need multiple instances. 😀

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 21, 2022, 10:10am UTC](https://forum.arduino.cc/t/making-timer-with-rtc-clock/1005807/21 "2022-12-21T10:10:16Z")

</div>

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