Https://forum.arduino.cc/t/how-do-i-set-a-48-hour-relay-trigger/571723

Hi Everyone. I saw the code on the link attached and couldn't reply on the post because it is already closed. I am developing similar project but for me it is irrigation system and I need to turn the pump on once every after 2 days for 2min. The code that I found on the forum works perfect but because in my country I experience lots of loadshadding (Power cut) almost everyday, this defeats the purpose of the code. I think I need a memory for that to happen (EEPROM maybe) :

#include <DS3231.h>

const byte RelayPin = 4;

DS3231  rtc(SDA, SCL);
Time t;
 static int lastMinute = 0;

const int OnHour = 22; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 33;
// const int OffHour = 22; //SET TIME TO OFF RELAY
// const int OffMin = 34;

void setup()
{
  Serial.begin(115200);
  rtc.begin();
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);
  lastMinute = EEPROM.read(0);
}

void loop()
{
 static int dayCounter = 0;  
 t = rtc.getTime();

  // Once per minute...
  if (t.min != lastMinute)
  {
    lastMinute = t.min;
    
    // ...Display the time...
    Serial.print(t.hour);
    Serial.print(" hour(s) , ");
    Serial.print(t.min);
    Serial.println(" minute(s) ");

    // ...And check for ON time...
    if (t.hour == OnHour && t.min == OnMin)
    {
      // ...But only act every 2nd day
      dayCounter++;
      if (dayCounter == 2)
      {
        dayCounter = 0;
        digitalWrite(RelayPin, HIGH);
        Serial.println("Pump ON");
        delay(120000UL); // Keep the pump ON (?) for two minute
        digitalWrite(RelayPin, LOW);
        Serial.println("Pump OFF");      
      }
    }
EEPROM.update(0, lastMinute);
  }
}

I have only added EEPROM syntax to store the lastMinute updated and to remember it when the Arduino switch on after power cut.

Firstly i am not sure if that's a good way of doing it, secondly the problem is, what would happen if the power cut happens at 22:00?

You have a RTC that has an alarm function that supports date. Set the alarm for the date and time you want it to trip. When you process the alarm reset it to the new time and date. You can set some flags to check if you have power,if so run the pump, if not set flag run pending or something. At reset, power restore, whatever check this flag and if true clear and run pump. This should get you started in the direction you need to go. You can keep this flag in EEPROM. Change the address dependent on the year and it should last for centuries. On new years make the new flag = the old.

I suspect that it's currently 3 times a day :frowning:

lastMinute is an int and takes two bytes. EEPROM.read() only reads one byte, EEPROM.update() only writes a single byte.

Use EEPROM.put() and EEPROM.get() instead. EEPROM.put(0,lastMinute) and EEPROM.get(0,lastMinute).

There might be other things wrong with your code but that's the first thing to fix.

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