Call a function once a second using the time() library?

Dillon1337
Posts: 2
Joined: Sat Mar 03, 2018 8:22 pm

Call a function once a second using the time() library?

Postby Dillon1337 » Sat Mar 03, 2018 8:36 pm

My code is heavily based on this example of receiving the time from the internet: https://github.com/espressif/arduino-es ... leTime.ino I'd like to call a function whenever the second increments. I'm using

Code: Select all

time(NULL)
to get the time and it's counting up in the background, so I'm wondering if there's something there that I can attach an interrupt to. Is this possible?

My alternative is to create a one second hardware timer interrupt and call the function there but I'd rather have my function exactly track the time.

Thanks in advance!
Dillon

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Call a function once a second using the time() library?

Postby kolban » Mon Mar 12, 2018 7:08 pm

I'm wondering (out loud) if this library might serve you:

Simple Timer

It claims to have both a setInterval() and a setTimeout() pair of functions that take a callback function and a duration.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Dillon1337
Posts: 2
Joined: Sat Mar 03, 2018 8:22 pm

Re: Call a function once a second using the time() library?

Postby Dillon1337 » Mon Mar 12, 2018 7:41 pm

It might work - thanks for the response. I'm unable to tell if it's blocking or not. I want to run other code in the foreground of the while loop.

I was able to make something work using a hardware timer (https://github.com/dwaq/Super7Seg/blob/ ... no#L31-L43), but ideally I would like to base something entirely on the time.h library (https://github.com/dwaq/Super7Seg/blob/ ... pp#L70-L80). I think it might be a linux/ RTOS standard, so maybe I'm better off asking someone with expertise on that topic.

Who is online

Users browsing this forum: No registered users and 75 guests