I'm trying to sequence through the alarm settings for a DS3231 RTC.
I'm using an LCD with a keypad and using the buttons to set the RTC.
I can set all other details but I can't seem to get the alarm mode to set.
I'm new to Bytes and so I could be making a glaring error there.
I've tried to just set the A1 alarm or both the alarms, it doesn't seem to make a difference.
I used the code from ArduinoChicken/alarmControl.ino at master · mlepard/ArduinoChicken · GitHub to set the alarm mode.
I'm using the DS3231 library DS3231 - Arduino Reference
Any help would be appreciated.
/*
DS3231_Alarm_Setting_Trial.pde
Guy Lowndes
2020-12-18
Information taken from DS3231_set and DS3231_test by Eric Ayars 4/11
Uses an LCD with keypad to set the time for a DS3231 RTC
uses code for the buttons from https://forum.arduino.cc/index.php?topic=354260.0 by Mark Bramwell, July 2010
Uses tips and tricks from https://www.baldengineer.com/arduino-lcd-display-tips.html
*/
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal.h>
DS3231 Clock;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
/*****************************************************************************************************************/
// These are the ALARM Bits that can be used
// They need to be combined into a single value (see below)
// Found here: https://github.com/mlepard/ArduinoChicken/blob/master/roboCoop/alarmControl.ino
#define ALRM1_MATCH_EVERY_SEC 0b1111 // once a second
#define ALRM1_MATCH_SEC 0b1110 // when seconds match
#define ALRM1_MATCH_MIN_SEC 0b1100 // when minutes and seconds match
#define ALRM1_MATCH_HR_MIN_SEC 0b1000 // when hours, minutes, and seconds match
#define ALRM1_MATCH_DY_HR_MIN_SEC 0b0000 // when hours, minutes, and seconds match
#define ALRM2_ONCE_PER_MIN 0b111 // once per minute (00 seconds of every minute)
#define ALRM2_MATCH_MIN 0b110 // when minutes match
#define ALRM2_MATCH_HR_MIN 0b100 // when hours and minutes match
/*****************************************************************************************************************/
// buffers for the display
char line0[21];
char line1[21];
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
const int btnRIGHT = 0;
const int btnUP =1;
const int btnDOWN =2;
const int btnLEFT =3;
const int btnSELECT =4;
const int btnNONE =5;
int bounceDelay;
int oldKey = 0;
/***************************************************************
* Set up and Loop
***************************************************************/
void setup() {
// Start the serial port
Serial.begin(9600);
Serial.println("Starting");
// Start the I2C interface
Wire.begin();
}
void loop() {
for (int i = 1; i<6; i++)
{
changeAlarmMethod1(true);
delay(2000);
}
}
void changeAlarmMethod1(bool increase) {
byte ALRM1_SET;
byte ALRM2_SET;
byte alarmBits;
String myString2="";
byte ADay1, AHour1, AMinute1, ASecond1, ABits1;
byte ADay2, AHour2, AMinute2, ABits2;
bool ADy1, A12h1, Apm1;
bool ADy2, A12h2, Apm2;
Clock.getA2Time(ADay1, AHour1, AMinute1, ABits2, ADy2, A12h2, Apm2);
ALRM2_SET=ABits2 >> 4;
Serial.print("A2 Current = ");
Serial.print(ALRM2_SET, BIN);
Serial.print(" ");
Clock.getA1Time(ADay1, AHour1, AMinute1, ASecond1, ABits1, ADy1, A12h1, Apm1);
ALRM1_SET=ABits1 & 0x0F;
Serial.print("A1 Current = ");
Serial.println(ALRM1_SET, BIN);
switch (ALRM1_SET){
case ALRM1_MATCH_EVERY_SEC: // once a second
{
Serial.println("ALRM1_MATCH_EVERY_SEC");
if (increase) {ALRM1_SET=ALRM1_MATCH_SEC;}
else {ALRM1_SET=ALRM1_MATCH_DY_HR_MIN_SEC;}
break;
}
case ALRM1_MATCH_SEC: // when seconds match
{
Serial.println("ALRM1_MATCH_SEC");
if (increase) {ALRM1_SET=ALRM1_MATCH_MIN_SEC;}
else {ALRM1_SET=ALRM1_MATCH_EVERY_SEC;}
break;
}
case ALRM1_MATCH_MIN_SEC: // when minutes and seconds match
{
Serial.println("ALRM1_MATCH_MIN_SEC");
if (increase) {ALRM1_SET=ALRM1_MATCH_HR_MIN_SEC;}
else {ALRM1_SET=ALRM1_MATCH_SEC;}
break;
}
case ALRM1_MATCH_HR_MIN_SEC:
{
Serial.println("ALRM1_MATCH_HR_MIN_SEC");
if (increase) {ALRM1_SET=ALRM1_MATCH_DY_HR_MIN_SEC;}
else {ALRM1_SET=ALRM1_MATCH_MIN_SEC;}
break;
}
case ALRM1_MATCH_DY_HR_MIN_SEC:
{
Serial.println("ALRM1_MATCH_DY_HR_MIN_SEC");
if (increase) {ALRM1_SET=ALRM1_MATCH_EVERY_SEC;}
else {ALRM1_SET=ALRM1_MATCH_HR_MIN_SEC;}
break;
}
}
byte newAlarmBits = ALRM2_SET;
newAlarmBits <<= 4;
newAlarmBits |= ALRM1_SET;
Clock.setA1Time(ADay1, AHour1, AMinute1, ASecond1, newAlarmBits, false, A12h1, Apm1);
Clock.setA2Time(ADay2, AHour2, AMinute2, newAlarmBits, false, A12h2, Apm2);
Serial.print("New alarm bits= ");
Serial.println(newAlarmBits, BIN);
Clock.getA2Time(ADay1, AHour1, AMinute1, ABits2, ADy2, A12h2, Apm2);
ALRM2_SET=ABits2 >> 4;
Serial.print("A2 New = ");
Serial.print(ALRM2_SET, BIN);
Serial.print(" ");
Serial.print("ABits2 New = ");
Serial.println(ABits2, BIN);
Clock.getA1Time(ADay1, AHour1, AMinute1, ASecond1, ABits1, ADy1, A12h1, Apm1);
ALRM1_SET=ABits1 & 0x0F;
Serial.print("A1 New = ");
Serial.println(ALRM2_SET, BIN);
Serial.print("ABits1 New = ");
Serial.println(ABits1, BIN);
}