Hello,
I m trying to found my way with the DS3231 RTC module: have read a lot of stuff on it and got a problem: I cannot manage to get both alarms work as interrupt to wake up my UNO twice a day (that is the goal)
if alarm1 and alarm2 are both activated, the interrupt occurs only when alarm2 trigger . if only one alarm is active it is working (even if it is alarm1 or alarm2). Both alarms are working fine and got the trigger flag when not in "interrupt mode".
My DS3231 module is a usual one ds3231 module with led and the 4 resistor block removed as exposed here: DS3231 modifications .
I m using the library from Rodan : DS3231 library by Rodan and this is my code (interrupt on pin 3 of my UNO)
It drives me crazy.. where I m wrong?
thx for your help!
#include "ds3231.h"
#include <Wire.h>
#include <LowPower.h>
#define BUFF_MAX 128
#define WAKEUPALARM 3 //2d wake up pin!!!!!!
struct ts t;
unsigned long SleepTime, previousSleepTime;
byte y, mth, d, h, m, s;
byte i, alarm1_sec,
alarm1_minute, alarm1_hour,
control_reg, status_reg,
alarm2_minute, alarm2_hour;
volatile bool flagAlarm = false;
void setup() {
pinMode (WAKEUPALARM, INPUT_PULLUP); //wake up pin 1 -> alarms
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Wire.begin();
// DS3231_init(DS3231_INTCN);
/*----------------------------------------------------------------------------
In order to synchronise your clock module, insert timetable values below !
----------------------------------------------------------------------------*/
// t.hour = 15;
// t.min = 39;
// t.sec = 0;
// t.mday = 19;
// t.mon = 03;
// t.year = 2021;
//
// DS3231_set(t);
//********************************Alarm *********************
// 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 flags[5] = { 0, 0, 0, 1, 1 };
uint8_t flagsA2[4] = { 0, 0, 1, 1 };
// set Alarm1&2
DS3231_set_a1(1, 15, 16, 0, flags); //(s, m,h,d,flag)
DS3231_set_a2(13, 16, 0, flagsA2); //( m,h,d,flag)
// activate Alarm1
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A1IE);
// activate Alarm2
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A2IE);
previousSleepTime = millis();
SleepTime = millis();
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("start");
delay(10);
DS3231_clear_a1f();
DS3231_clear_a2f();
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
if ((SleepTime - previousSleepTime) > 20000) {
// Allow wake up pin to trigger interrupt on low.
digitalWrite(LED_BUILTIN, LOW);
Serial.println("sleeping test .....");
delay(10);
attachInterrupt(1, wakeUp, LOW); // alarm on pin3 , FALLING or LOW ?
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is low.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// Disable external pin interrupt on wake up pin.
//HERE AFTER WAKING UP
detachInterrupt(1);
Serial.print("reveil!!!");
previousSleepTime = millis();
}
digitalWrite(LED_BUILTIN, HIGH);
DS3231_get(&t);
char buff[BUFF_MAX];
h = t.hour;
m = t.min;
s = t.sec;
Serial.print("\t Hour : ");
Serial.print(h);
Serial.print(":");
Serial.print(m);
Serial.print(".");
Serial.println(s);
delay(2000);
// display a1 debug info
// DS3231_get_a1(&buff[0], 59);
// Serial.println(buff);
// alarms_read_display();
if (DS3231_triggered_a1()) {
// INT has been pulled low
Serial.println(" -> alarm1 has been triggered");
Serial.println(String(flagAlarm));
DS3231_clear_a1f();
}
if (DS3231_triggered_a2()) {
// INT has been pulled low
Serial.println(" -> alarm2 has been triggered and I do the stuff!!");
DS3231_clear_a2f();
}
delay(1000);
SleepTime = millis();
}
void alarms_read_display() { // Function to read and display alarm1, alarm2 and temperature data
Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
Wire.write(0x07); // Send register address (0x07) for second (0x08) for starting at minute
Wire.endTransmission(false); // I2C restart
Wire.requestFrom(0x68, 4); // Request 4 bytes (corresponding to Alarm1) from DS3231 and release I2C bus at end of reading
alarm1_sec = Wire.read();
alarm1_minute = Wire.read(); // Read alarm1 minutes
alarm1_hour = Wire.read(); // Read alarm1 hours
Wire.read(); // Skip alarm1 day/date register
Wire.beginTransmission(0x68); // Start I2C protocol with DS3231 address
Wire.write(0x0B); // Send register address (0x07) for second (0x08) for starting at minute
Wire.endTransmission(false); // I2C restart
Wire.requestFrom(0x68, 3);
alarm2_minute = Wire.read(); // Read alarm2 minutes
alarm2_hour = Wire.read(); // Read alarm2 hours
Wire.read(); // Skip alarm2 day/date register
// control_reg = Wire.read(); // Read the DS3231 control register
// status_reg = Wire.read(); // Read the DS3231 status register
// Wire.read(); // Skip aging offset register
// temperature_msb = Wire.read(); // Read temperature MSB
// temperature_lsb = Wire.read(); // Read temperature LSB
// Convert BCD to decimal
alarm1_sec = ((alarm1_sec / 16 * 10) + (alarm1_sec % 16));
alarm1_minute = ((alarm1_minute / 16 * 10) + (alarm1_minute % 16));
alarm1_hour = ((alarm1_hour / 16 * 10) + (alarm1_hour % 16));
alarm2_minute = ((alarm2_minute / 16 * 10) + (alarm2_minute % 16));
alarm2_hour = ((alarm2_hour / 16 * 10) + (alarm2_hour % 16));
Serial.print("alarm1 sec: ");
Serial.println(alarm1_sec, DEC);
Serial.print("alarm minute ");
Serial.println(alarm2_minute);
Serial.print("alarm2 hours ");
Serial.println(alarm2_hour);
}
void wakeUp()
{
// Just a handler for the pin interrupt.
flagAlarm = true;
}