Created
October 6, 2020 00:19
-
-
Save mgruben/5c6637e0a53e1aa41256fc0862838533 to your computer and use it in GitHub Desktop.
Feed-the-Fish Timer using RTC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <DS3232RTC.h> | |
#include <LowPower.h> | |
// pin definitions | |
const int SQW_PIN = 2; | |
const int BUTTON_PIN = 3; | |
const int LED_PIN = 8; | |
// Keep some state for later clearing the alarm | |
volatile boolean alarm_called = false; | |
void turnLedOn() { | |
digitalWrite(LED_PIN, HIGH); | |
// Would perhaps be more convenient to call RTC.alarm(ALARM_2) here, | |
// but that breaks this ISR. Accordingly, instead keep track of | |
// whether we've been in this ISR, and if so clear the alarm flag | |
// on the DS3231 | |
alarm_called = true; | |
} | |
void turnLedOff() { | |
digitalWrite(LED_PIN, LOW); | |
} | |
void setup() | |
{ | |
// BOILERPLATE: https://github.com/JChristensen/DS3232RTC/blob/master/examples/alarm_ex1/alarm_ex1.ino | |
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags | |
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1); | |
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1); | |
RTC.alarm(ALARM_1); | |
RTC.alarm(ALARM_2); | |
RTC.alarmInterrupt(ALARM_1, false); | |
RTC.alarmInterrupt(ALARM_2, false); | |
RTC.squareWave(SQWAVE_NONE); | |
// END BOILERPLATE | |
// Define our pins | |
pinMode(SQW_PIN, INPUT_PULLUP); | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
pinMode(LED_PIN, OUTPUT); | |
// Hack to turn off the "L" LED? | |
pinMode(13, OUTPUT); | |
digitalWrite(13, LOW); | |
// set Alarm 2 to occur every day at 7:00 AM | |
// https://github.com/JChristensen/DS3232RTC/blob/master/examples/alarm_ex7/alarm_ex7.ino | |
RTC.setAlarm(ALM2_MATCH_HOURS, 0, 0, 7, 0); | |
// clear the alarm flag | |
// https://github.com/JChristensen/DS3232RTC/blob/master/examples/alarm_ex7/alarm_ex7.ino | |
RTC.alarm(ALARM_2); | |
// configure the INT/SQW pin for "interrupt" operation (disable square wave output) | |
RTC.squareWave(SQWAVE_NONE); | |
// enable interrupt output for Alarm 2 | |
RTC.alarmInterrupt(ALARM_2, true); | |
attachInterrupt(digitalPinToInterrupt(SQW_PIN), turnLedOn, FALLING); | |
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), turnLedOff, FALLING); | |
} | |
void loop() | |
{ | |
// If we've woken up, check if we need to clear Alarm 2 | |
if (alarm_called) | |
{ | |
RTC.alarm(ALARM_2); | |
alarm_called = false; | |
} | |
// On the off chance that we happen to be connected to a serial port, | |
// Try to write the time to the serial port. | |
Serial.begin(9600); | |
setSyncProvider(RTC.get); // the function to get the time from the RTC | |
if(timeStatus() != timeSet) | |
Serial.println("Unable to sync with the RTC"); | |
else | |
Serial.println("RTC has set the system time"); | |
digitalClockDisplay(); | |
Serial.end(); | |
// Given the serial output time to finish printing | |
delay(300); | |
// Put the Arduino into a low-power state | |
// We will use interrupts to wake it back up | |
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); | |
} | |
// https://github.com/JChristensen/DS3232RTC/blob/master/examples/TimeRTC/TimeRTC.ino | |
void digitalClockDisplay() | |
{ | |
// digital clock display of the time | |
Serial.print(hour()); | |
printDigits(minute()); | |
printDigits(second()); | |
Serial.print(' '); | |
Serial.print(day()); | |
Serial.print(' '); | |
Serial.print(month()); | |
Serial.print(' '); | |
Serial.print(year()); | |
Serial.println(); | |
} | |
// https://github.com/JChristensen/DS3232RTC/blob/master/examples/TimeRTC/TimeRTC.ino | |
void printDigits(int digits) | |
{ | |
// utility function for digital clock display: prints preceding colon and leading 0 | |
Serial.print(':'); | |
if(digits < 10) | |
Serial.print('0'); | |
Serial.print(digits); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment