Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created February 8, 2016 00:19
Show Gist options
  • Select an option

  • Save dwblair/ba5572d622e9ba1bd996 to your computer and use it in GitHub Desktop.

Select an option

Save dwblair/ba5572d622e9ba1bd996 to your computer and use it in GitHub Desktop.
/*----------------------------------------------------------------------*
* Demonstrates usage of the alarm interrupt for alarm 1 and alarm 2. *
* *
* 26 December 2014. *
* *
* Tested with Arduino 1.5.8, Arduino Nano w/ ATmega328, DS3231. *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Streaming.h> //http://arduiniana.org/libraries/streaming/
#include <Time.h> //http://playground.arduino.cc/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
#define SQW_PIN 2
int intervalMinutes = 1;
void setup(void)
{
Serial.begin(115200);
//setSyncProvider() causes the Time library to synchronize with the
//external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << "RTC Sync";
if (timeStatus() != timeSet){
Serial << " FAIL!";
}
Serial << endl;
printDateTime( RTC.get() );
Serial << " --> Current RTC time." << endl;
//Disable the default square wave of the SQW pin.
RTC.squareWave(SQWAVE_NONE);
//Attach an interrupt on the falling of the SQW pin.
//digitalWrite(SQW_PIN, HIGH); //redundant with the following line
pinMode(SQW_PIN, INPUT_PULLUP);
attachInterrupt(INT0, alarmIsr, FALLING);
// set the alarm to go off initially 5 seconds from now
int seconds = second(RTC.get());
int alarmSeconds = (seconds+5)%60;
//Set an alarm at every 20th second of every minute.
RTC.setAlarm(ALM1_MATCH_SECONDS, alarmSeconds, 0, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
/*
//Set an alarm every minute.
RTC.setAlarm(ALM2_MATCH_MINUTES, 0, 2, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_2); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_2, true);
*/
}
volatile boolean alarmIsrWasCalled = false;
void alarmIsr()
{
alarmIsrWasCalled = true;
}
void loop(void)
{
if (alarmIsrWasCalled){
if (RTC.alarm(ALARM_1)) {
printDateTime( RTC.get() );
Serial << " --> Alarm 1!" << endl;
int minutes = minute(RTC.get());
int nextAlarmMinute=findNextMinutes(minutes, intervalMinutes);
Serial.print("Next alarm goes off at minutes=");
Serial.println(nextAlarmMinute);
RTC.setAlarm(ALM1_MATCH_MINUTES, 0, nextAlarmMinute, 0, 1); //daydate parameter should be between 1 and 7
RTC.alarm(ALARM_1); //ensure RTC interrupt flag is cleared
RTC.alarmInterrupt(ALARM_1, true);
}
if (RTC.alarm(ALARM_2)) {
printDateTime( RTC.get() );
Serial << " --> Alarm 2!" << endl;
}
alarmIsrWasCalled = false;
}
/*
delay(1000);
int seconds = second(RTC.get());
Serial.println(seconds);
*/
}
void printDateTime(time_t t)
{
Serial << ((day(t)<10) ? "0" : "") << _DEC(day(t)) << ' ';
Serial << monthShortStr(month(t)) << " " << _DEC(year(t)) << ' ';
Serial << ((hour(t)<10) ? "0" : "") << _DEC(hour(t)) << ':';
Serial << ((minute(t)<10) ? "0" : "") << _DEC(minute(t)) << ':';
Serial << ((second(t)<10) ? "0" : "") << _DEC(second(t));
}
int findNextMinutes(int input, int interval) {
return (input + interval)%60;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment