Alarm time settings on a DS3231

Hello again,

Removed the diode as per suggestion and modified the code a bit. The alarm trips on time but, if power is removed from the Arduino, the alarm time settings are lost. The battery is at 3.7V with Arduino off.

Here’s a simple code where the problem can be reproduced…

#include <DS3231.h>

/* Clock chip settings */
DS3231 Clock;
byte ADay, AHour, AMinute, ASecond, ABits; // Alarm variables
bool ADy, A12h, Apm;  // Alarm variables

// setting AL1 and AL2 registers
// http://forum.arduino.cc/index.php?topic=168421.0
#define ALRM1_MATCH_HR_MIN_SEC 0b1000  // when hours, minutes, and seconds match
byte ALRM1_SET = ALRM1_MATCH_HR_MIN_SEC;
#define ALRM2_MATCH_HR_MIN     0b100   // when hours and minutes match
byte ALRM2_SET = ALRM2_MATCH_HR_MIN;
// Set AlarmBits, ALRM2 first, followed by ALRM1

String readString;            // storing data received from serial

void setup()
{
  Serial.begin(9600);
  delay(50);
}

void loop()
{
  Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm); // read alarm stored values
  Serial.print(AHour, DEC); Serial.print(":"); Serial.println(AMinute, DEC);  // print alarm time erroneous
  delay(2000); // so I can see it

  setAlarmTime();
  Serial.print(AHour); Serial.print(":"); Serial.println(AMinute);  // prints alarm time ok
  delay(1000);

}

void setAlarmTime() {

  while (Serial.available() )     // Send data only when you receive data
  {
    delay(3);
    char c = Serial.read();        //Read the incoming data
    readString += c;
  }
  if (readString.length() > 0) {
    delay(100);
    Serial.println(readString);          //print value on Serial monitor to ensure data is correct text => "alHHMM" ex.: al1534 => 15:34 

    /*set alarm hours/mins */
    if (readString.substring(0, 2) == "al") { // are the first 2 chars = "al"?
      if (readString.length() == 6) { // if so storethe time in the variables
        AHour = readString.substring(2, 4).toInt();
        AMinute = readString.substring(4, 6).toInt();
        setAlarms(); // move variables data to DS registers
      }
    }
  }

  readString = "";  // clear variable

}


void setAlarms() {

  // Set AlarmBits, ALRM2 first, followed by ALRM1
  int AlarmBits = ALRM2_SET;
  AlarmBits <<= 4;
  AlarmBits |= ALRM1_SET;

  // set both alarms to :00 and :30 seconds, every minute
  // Format: .setA*Time(DoW|Date, Hour, Minute, Second, 0x0, DoW|Date, 12h|24h, am|pm)
  //                    |                                    |         |        |
  //                    |                                    |         |        +--> when set for 12h time, true for pm, false for am
  //                    |                                    |         +--> true if setting time based on 12 hour, false if based on 24 hour
  //                    |                                    +--> true if you're setting DoW, false for absolute date
  //                    +--> INTEGER representing day of the week, 1 to 7 (Monday to Sunday)
  //
  Clock.setA1Time(Clock.getDoW(), AHour, AMinute, 0, AlarmBits, false, false, false);
  Clock.setA2Time(Clock.getDate(), AHour, AMinute, AlarmBits, false, false, false);

  // Turns alarms on
  Clock.turnOnAlarm(1);
  Clock.turnOnAlarm(2);
}