I am trying to develop an Arduino based alarm using the DS3231.
I found this code on the web, not sure location as not commented.
The program works fine using the A1 alarm, but I figured out that by renaming the alarms and flags I could use tboth alarms, so I called alarm 1= a1 and alarm 2= a2 and changed the corresponding flags flags1 and flags2.
When the time gets to the values set for alarm1 it displays the message that the alarm has been triggered, but does not make INT/SQW go LOW. When it reaches the alarm2 time, it does not do anything.
I guess that its something to do with the if statements, but as I am still learning, I am not clever enough to work out how to correct this.
Can anyone help?
Here is the Code:
// during an alarm the INT pin of the RTC is pulled low
//
// this is handy for minimizing power consumption for sensor-like devices,
// since they can be started up by this pin on given time intervals.
#include <Wire.h>
#include "ds3231.h"
#define BUFF_MAX 256
// time when to wake up
uint8_t wake1_HOUR = 18;
uint8_t wake1_MINUTE = 03;
uint8_t wake1_SECOND = 00;
uint8_t wake2_HOUR = 18;
uint8_t wake2_MINUTE = 04;
// how often to refresh the info on stdout (ms)
unsigned long prev = 5000, interval = 5000;
void set_alarm(void)
{
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm - see datasheet
// A1M1 (seconds) (0 to enable, 1 to disable)
// A1M2 (minutes) (0 to enable, 1 to disable)
// A1M3 (hour) (0 to enable, 1 to disable)
// A1M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags1[5] = { 0, 0, 0, 1, 1 };
// A2M1 (seconds) (0 to enable, 1 to disable)
// A2M2 (minutes) (0 to enable, 1 to disable)
// A2M3 (hour) (0 to enable, 1 to disable)
// A2M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags2[5] = { 1, 0, 0, 0, 1 };
// set Alarm1
DS3231_set_a1(wake1_SECOND, wake1_MINUTE, wake1_HOUR, 0, flags1);
// activate Alarm1
DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
// set Alarm2
DS3231_set_a2(wake2_MINUTE, wake2_HOUR, 0, flags2);
// activate Alarm
DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
}
void setup()
{
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
DS3231_clear_a1f();
set_alarm();
}
void loop()
{
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// once a while show what is going on
if ((now - prev > interval) && (Serial.available() <= 0)) {
DS3231_get(&t);
// display current time
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,
t.mon, t.mday, t.hour, t.min, t.sec);
Serial.println(buff);
// display a1 debug info
DS3231_get_a1(&buff[0], 59);
Serial.println(buff);
if (DS3231_triggered_a1()) {
// INT/SQW has been pulled low
Serial.println(" -> alarm1 has been triggered");
// clear a1 alarm flag and let INT go into hi-z
//DS3231_clear_a1f();
}
if (DS3231_triggered_a2()) {
// INT/SQW has been pulled low
Serial.println(" -> alarm has been triggered");
// clear a1 alarm flag and let INT go into hi-z
DS3231_clear_a1f();
}
prev = now;
}
}