I'm doing a project for my brother witch is an automatic watering thing, it's all working fine but I just don't know how to repeat the code to make the relay say come on for 5min at 0900 than 5min at 1200 and also 5min at 1700. how do I do this, I'm not very good at this so I need clear instructions or someone to write it. pls help. thank you, James.

here is the code I need to be repeated to meet what I need done, basically I just need the relay to trigger every 4h for 5min. its on an rtc on an Arduino UNO.

#include <DS3231.h>

int Relay = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 19; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 55;
const int OffHour = 19; //SET TIME TO OFF RELAY
const int OffMin = 56;

void setup() {
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);

void loop() {
  t = rtc.getTime();
  Serial.print(" hour(s), ");
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);

  if(t.hour == OnHour && t.min == OnMin){
    Serial.println("LIGHT ON");

    else if(t.hour == OffHour && t.min == OffMin){
      Serial.println("LIGHT OFF");
  • why don't you simply add two more if blocks?
    – jsotola
    Oct 4, 2019 at 16:13
  • no matching function for call to 'DS3231::DS3231(const uint8_t&, const uint8_t&) please help me out Jan 24, 2020 at 6:26

1 Answer 1


You will most likely find nobody here, who will write the code for you, since this is not a free code writing service. But I will suggest a good way.

I just need the relay to trigger every 4h for 5min

The phrase "every x hours" screams for using the modulo operator %. Modulo gives you the rest value of an integer division. See these values:

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
7 % 4 = 3
8 % 4 = 0

You see, the modulo 4 operation gives you zero every 4 hours starting with midnight. So make an if statement based on that

if(hour % 4 == 0 && min == 0)

to switch on and

if(hour % 4 == 0 && min == 5)

to switch off. Just change your code in this way and you are done.

  • Thankyou, I will give it a try tomorrow and see how it goes then let you know.
    – james
    Oct 4, 2019 at 13:53
  • Also were do I put all the 0%4 things?
    – james
    Oct 4, 2019 at 14:06
  • In your code you already have if statements for turning the relay on and off. And I included corresponding if statements using the modulo operator in my answer. These if statements can be used to turn the relay on and off. It is your part, to write that in your code. If something in my answer is not clear, please specify what, so that I can better explain how this works.
    – chrisl
    Oct 4, 2019 at 14:13
  • I understand were the statements go but I have no idea were to put the list of values, as I sayed I'm pretty dumb when it comes to this so I'm sorry if I'm being a bit stubborn. I'm young and don't have a lot of knowledge on how to write this. Your hell so far is very much appreciated.
    – james
    Oct 4, 2019 at 14:38
  • The list of values doesn't belong in your code. I've just included it to show how the modulo operator works
    – chrisl
    Oct 4, 2019 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.