Unable to Adjust RTC Time

I am attempting to use a clock with two pushbuttons. However, the buttons, when pressed, reset the time to 0 as opposed to incrementing the hour or minute. I am using Henning Karlsen’s DS3231 library. Here is my code.

#include <DS3231.h>

#define RED_PIN 44  //RGB LED IS COSMETIC ONLY
#define GRN_PIN 45
#define BLU_PIN 46
#define HOUR_BUTTON 8
#define MINUTE_BUTTON 7

//Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);

Time Clock;

//uint8_t& Stunde = Clock.hour; //Stunde = hour
//uint8_t& Minute = Clock.min;

void setup()
{
  //Set up Serial connection
  Serial.begin(115200);

  //Initialize the rtc bject
  rtc.begin();

  //The following lines can be uncommented to set the date and time
//  rtc.setDOW(SATURDAY);     // Set Day-of-Week
//  rtc.setTime(16, 4, 00);     // Set the time
//  rtc.setDate(27, 4, 2019);   //Day, Month, Year

  //The buttons have internal pullup resistors, "flipping" the high-low logic
  pinMode(HOUR_BUTTON, INPUT_PULLUP);
  pinMode(MINUTE_BUTTON, INPUT_PULLUP);
}

void loop()
{
  Time Clock;
//  uint8_t& Stunde = Clock.hour; //Stunde = hour
//  uint8_t& Minute = Clock.min;
  static int i = 0;

  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");

  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  // Determine what color to light the LED (COSMETIC ONLY)
  switch (i) {
    case 0: analogWrite(RED_PIN, 255); analogWrite(GRN_PIN, 0); analogWrite(BLU_PIN, 0); i++; break;
    case 1: analogWrite(RED_PIN, 0); analogWrite(GRN_PIN, 255); analogWrite(BLU_PIN, 0); i++; break;
    case 2: analogWrite(RED_PIN, 0); analogWrite(GRN_PIN, 0); analogWrite(BLU_PIN, 255); i++; break;
    case 3: analogWrite(RED_PIN, 255); analogWrite(GRN_PIN, 255); analogWrite(BLU_PIN, 0); i++; break;
    case 4: analogWrite(RED_PIN, 255); analogWrite(GRN_PIN, 0); analogWrite(BLU_PIN, 255); i++; break;
  }

  //Prevent the switch statement from receiving an out-of-range value
  if (i > 4) {
    i = 0;
  }

  if (digitalRead(HOUR_BUTTON) == LOW) {
    AddHour(Clock.hour);
    rtc.setTime(Clock.hour, Clock.min, Clock.sec);

    //Debug lines
    Serial.println(Clock.hour);
    Serial.println("Hour button pressed");
  }

  if (digitalRead(MINUTE_BUTTON) == LOW) {
    AddMinute(Clock.min);
    rtc.setTime(Clock.hour, Clock.min, Clock.sec);

    //Debug lines    
    Serial.println(Clock.min);
    Serial.println("Minute button pressed");
  }

  if (digitalRead(HOUR_BUTTON) == LOW && digitalRead(MINUTE_BUTTON) == LOW) {
    ResetSeconds();
  }

  //Wait one second before repeating
  delay(1000);
}

static int AddMinute(int argMinute) {
  Clock.min = argMinute + 1;
  return Clock.min;
}

static int AddHour(int Hour) {
  Clock.hour = Hour + 1;
  return Clock.hour;
}

static void ResetSeconds() {
  Clock.sec = 0;
  return Clock.sec;
}

Thank you in advance.

Your problem is you have to variables named 'Clock' and one is global and one is local to loop().

When you call

    AddHour(Clock.hour);

inside loop(), you are passing it the local version

Inside your AddHour() function, you are referencing your global version so that is what gets incremented

Upon returning from that function, you then call

    rtc.setTime(Clock.hour, Clock.min, Clock.sec);

which again references the local version

I would get rid of the local variable inside loop() and then everything will be dealing with the global variable.

@blh64, Thank you SO much for your help! :slight_smile: I completely forgot about the Clock object local to loop(). The increment functions now work as intended.