Matching the Arduino UNO accurately to a clock

For a project, I need the Arduino UNO to switch a relay for 2 seconds every 30 minutes. It needs to keep accurate time with a clock. (It is to activate an automata display accurately on the hour and half hour).

Any suggestions on how I can achieve this would be appreciated.

Kind regards

John

ps. I'm at slightly advanced beginner level.

Thank you for that info HoldingPattern. I will digest and hope I understand it. First glance tells me that I'll get there.

John

I now have a DS 3231 hooked up and Arduino recognises it.

I tried running the suggested script above kindly posted by holdingpattern but it comes back with an error message. Being new to programming, I’m not sure where to go from here. Should I have the ‘missing’ file (DS3231.h: No such file or directory) stored somewhere?

Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: “Arduino Uno”

CLOCK:8:10: fatal error: DS3231.h: No such file or directory

#include <DS3231.h>

^~~~~~~~~~

compilation terminated.

exit status 1
DS3231.h: No such file or directory

If I remove that line, I then get -

Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: “Arduino Uno”

CLOCK:10:1: error: ‘DS3231’ does not name a type; did you mean ‘CS21’?

DS3231 Clock;

^~~~~~

CS21

C:\Users\John\Documents\Arduino\CLOCK\CLOCK.ino: In function ‘void doRelay()’:

CLOCK:40:18: error: ‘Clock’ was not declared in this scope

Serial.println(Clock.getMinute());

^~~~~

exit status 1
‘DS3231’ does not name a type; did you mean ‘CS21’?

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

You need to install the library. Google "Arduino install library" for tutorials.

Thank you jremington :slight_smile: That worked. I'm on a steep learning curve :slight_smile:

holdingpattern:
Use an RTC chip like DS3231, and if minute is 0 or 30, turn the relay on. Either capture millis() into a start-time variable and monitor for 2s elapsed, or just use a delay() if that's no big deal.

Also set a flag to say relay is on, and only turn it on if the flag is false, else it will run over and over as long as the minute is 0 or 30. Then clear the flag when minute's not 0 or 30, for next time.

Another way to do this, is store the minute in something like "previousMinute". When you read the current minute, compare it with previousMinute. If it has changed, test it and execute code if needed, and set previousMinute to the current minute. Otherwise do nothing.

In my opinion, it is more straightforward than setting a flag.

I've got both sketches working thank you. The second one is easier for me to understand and I have changed it to activate on the quarter hours in case that is required.

Just one thing I can't sort is getting the minutes to match the actual time in the serial monitor. The relay is activating in time with the right hand number but I can't get that to match the real time on the left.

I've tried to set the time on the DS3231 but I must be doing something wrong. I presume the left hand time on the serial monitor is from my PCs clock ?

John

14:58:39.302 -> 24
14:58:39.302 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24
14:58:39.335 -> 24

A big thank you for the help given above. It gave me the leg-up that I needed. :slight_smile: :slight_smile: :slight_smile:

I've changed the program from just a 2 second on-time every 30 mins to a full one minute sequence every 15 seconds. It is working a bank of 8 relays and it's working without fault. I don't profess to understand all the coding but enough to get me started.

A question: In the code written by you, holdingpattern, there are twin vertical lines. How do I type them and what do they do within the program?

Regards
John

Serial.println(Clock.getMinute());
//new minute?
if (Clock.getMinute() != previousMinute) //yes, new minute
if (Clock.getMinute() == on1 || Clock.getMinute() == on2 || Clock.getMinute() == on3 || Clock.getMinute() == on4) // is the new minute the "right" minute?
{
digitalWrite(Music, HIGH);
delay (3000); // Allow Music to Start first.
digitalWrite (Motor1, HIGH); // Start Automata Sequence
digitalWrite (Lights1, HIGH);
delay (4000);
digitalWrite (Motor1, LOW);
delay (2000);
digitalWrite (Lights1,LOW);
delay (2000);

Thanks again holdingpattern. Found it on mine - bottom left by the Ctrl key. Never used it before.

Now I've got this project out of the way, I can go back to my original plan of learning the program etc at a more leisurely pace over a period.

I'm hoping this will be the last time I ask the forum for a while. :slight_smile:

Regards
John

holdingpattern:
The || means "or"

It does, and you can simply say, "or" if you like. It is exactly the same.

 if (Clock.getMinute() == on1 or Clock.getMinute() == on2 or Clock.getMinute() == on3 or Clock.getMinute() == on4)