How to write code for 48 hour interval using ds3231 and Arduino uno

I'm facing problem in bird feeder project.
I need 48 hours interval to fill feeding. I am using ds3231 rtc module. How can I write code for it

Have you tried to use your RTC with the example sketch in the library ?

Use the IDE library manager to install a DS3231 library and run some of the examples that come with the library to understand how the RTC and library works.

As others have suggested, get familiar with the module first. In particular how to set an alarm, as this can be used to trigger your feeder.

You can't set an alarm to trigger every 48 hours, but you can set it to trigger when the time and day of the week matches. As per the last row in table below (from the DS3231 Datasheet)...

The easiest way to trigger every 48 hours, is after the alarm triggers, set the next alarm by triggering 2 days later. That is, set the "Alarm 1 Day" setting to today's value + 2 (max 7).

I guess the other option is to just have an alarm every 24 hours, but ignore every 2nd alarm. You would need to keep track of the alarm count, and ensure this persisted if you lost power (for example by using the EEPROM).

1 Like
#include "RTClib.h" //adafruit lib
RTC_DS3231 rtc;

bool isDoneForToday = false;
const byte Feeder_pin = 5;

void setup () {
  while (! rtc.begin()) {
    digitalWrite(13, HIGH);
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
  }
}

void loop () {
  DateTime now = rtc.now();
  if ( isDoneForToday) {
    if (now.hour() != 8) isDoneForToday = false;
  } else {
    if (now.hour() == 8 ) {
      digitalWrite(Feeder_pin, HIGH);
      delay(200);
      digitalWrite(Feeder_pin, LOW);
      isDoneForToday = true;
    }
  }
  delay(1800000UL);
}

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