Wanting to perform actions during certain times of day - how does one code this?

Hi All

Firstly... Happy Easter Monday to everyone!

OK, so onto my question. I want to set up some code that carries out certain actions duing certain times of the day. So say be able to turn a digital pin on/off only during 19:00-23:00 etc.

Is there a function or Library for date and time calculations please?

Ta

Time.h / Timelib.h

The Arduino does not have a clock; so the closest you can get is some form of lapsed time since power on / reset.

To have a real time, you need a RTC.

RTC modules are cheap and easy to use. Here is one example of using them.

Great info here, thanks both.

I genuinely though this was a coding query hence posting originally in Programming Questions.

John, alternatives to using an rtc module:

if you have a type of Arduino that has wifi or Ethernet, you can program the Arduino to get the time from a Network Time Sever, then use it's internal timer to maintain the time after that. This will eventually drift away from the correct time, but you can re-sync with the network once per day, for example.

You can get modules that receive the time from long wave radio transmitters.

But an rtc module is probably simplest for beginners. I would recommend a ds3231 module over a ds1307 module, they are about the same price but much more accurate.

2 Likes

Thanks Paul.

I only have two UNO's and a MINI Pro, but I do think an Arduino with WiFi or Ethernet capability is the next step up in my learning curve. I have done the tempersature stuff, the relay stuff, the PIR stuff, the BlueTooth stuff :slight_smile: - still lots and lots to learn about coding: what functions are available, syntax etc...

I still have to set up my BMP280 Barometric sensor, and the LED lighting controller, but I am really enjoying this journey though, fair play.

I have even modded an ATX PSU from an old PC I hadn't used for 6 years! Check me eh! :slight_smile:

John, when you want to take that next step, this is what I personally would recommend:

Wemos D1 mini

I am not paid by Wemos to sell their products or anything, but I have completed several projects using them.

Weather Station

Indoor temp/humidity Sensors

theMusicMan:
Hi All

Firstly... Happy Easter Monday to everyone!

OK, so onto my question. I want to set up some code that carries out certain actions duing certain times of the day. So say be able to turn a digital pin on/off only during 19:00-23:00 etc.

Is there a function or Library for date and time calculations please?

Time calculations are easy.

Let's assume:
int switchOnTime=1900; // meaning 19:00
int switchOffTime=2300; // meaning 23:00
and having current hour and minute in variable h and m:

int currentTime=100*h+m;
Then you simply calculate:
bool switchState=(currentTime>=switchOnTime && currentTime<switchOffTime);
Then switch something ON or OFF
digitalWrite(switchPin,switchState);

There is nothing complicated with this, without using any library.

In case you have more than one switchOnTime and switchOffTime per day, you'd have to use an array of switching times and the switching logic requires two more lines of code.
Not tooo complicated.
Even if you want to have "switching over midnight, meaning: switch ON before midnight and switch OFF after midnight. All very easy. Let me know if you need another example for more than one witching time per day.

1 Like

PaulRB:
John, when you want to take that next step, this is what I personally would recommend:

Wemos D1 mini

I am not paid by Wemos to sell their products or anything, but I have completed several projects using them.

Weather Station

Indoor temp/humidity Sensors

Thanks Paul, this looks very interesting and I am particularly interested in learning all about this IoT malarkey...

I still have a lot of 'ideas' re what I want to do with my Arduino's, including my just-because-I-want-to-and-can project re building some form of PID control rig - not decided exactly yet what I will build, but it is something that has interested me for years (I have another thread specifically on that subject).

I love the idea of 'interfacing' to the real world via Mac, too... so... :slight_smile: excited!

PaulRB:
John, when you want to take that next step, this is what I personally would recommend:

Wemos D1 mini

Hey Paul - I have had a peruse around the web for a WiFi shield board, and a quick qn I have for you is... is the Wemos board you linked to pretty much the same thing as this WiFi Shield from FluxWorkshop on eBay?

I appreciate it's pricey and more expensive than the Wemos device, but... it is also available this week, and so far I have received superb service from Sebastia,n who owns that UK eBay store.

If it is the same sort of thing... I may buy the one from UK :slight_smile:

Impatient bugger aren't I?

EDIT: I just foind this Wemos one too... same thing, Paul?

Thanks

John, neither of your links work.

But I can tell you this. The Wemos is not a shield. It does not attach to an Uno/Mega/Due, it replaces them. If this sounds miraculous, well, it isn't. The Wemos is just another esp8266 based development board. There are quite a few others around, but I find the Wemos to be the best balance for me of price, simplicity and size.

Ah drat, sorry Paul. Must be because I was logged in to eBay when I copied them, sorry!

I have been a busy bee reading loads of stuff related to the ESP8266 and it seems i have a dilemma. I need one that DOESN'T use the CH340 chipset as that is totally incompatible with my Mac. I recently bought a Nano and had to return it becasue I could nto get any serial comms working.

So... at the risk of posting this question in several threads (sorry Mods), if you can advise I'd be eternally grateful. Well... maybe not eternally, but very grateful nonetheless :slight_smile:

What about cp2104? The newest Wemos Mini Pro uses that instead of the ch340.

That's awesome, Paul, thanks for this info as it's exactly what I'd need to purchase.

Now to find one in U.K. and ensure it's the latest version!

Boom! Found straight away on eBay - delivery Thursday. Yessssss

PaulRB:
John, when you want to take that next step, this is what I personally would recommend:

Wemos D1 mini

I am not paid by Wemos to sell their products or anything, but I have completed several projects using them.

Weather Station

Indoor temp/humidity Sensors

Just to let you know Paul - I bought a Wemos D1 Pro Mini this morning. Arriving on Thursday. Can't wait to try all this IoT stuff out!