Incrementing hours and minutes on alarm clock

Hi this is my first time posting in the forum so I apologize in advance for my newness. I code a little and I really want some experience using arduino so I'm using an uno to make an RTC alarm clock. I have an LCD screen and 3 buttons in pullup configuration and an SPST switch attached. The buttons are Set/snooze, increment hours and increment minutes. The switch lets the user pick between setting the time or setting an alarm. The default screen always shows the time and date.

From the default screen, the user holds the set button for 2 seconds to enter either the time or alarm set mode depending on the position of the switch. I want to increment hours and minutes the same way for both settings and simply save them into their own variables and eventually compare them to sound an alarm. My problem is the hours and minutes do not increment when I push either the hours or minutes button. I understand I have not coded a way for the user to get back to the default screen. I was thinking a 10 second timeout after no button pushes is best but I can't get anything to increment. I can display the time and date and holding the set button for 2 seconds enters the correct modes but the hours and minutes buttons do nothing.

I'm using the DS1307 breakout board from adafruit and I'm also using RTClib. I'm planning to switch these buttons to the analog pins after I add the wave shield from adafruit as well to sound the alarm. I've only been testing for the alarm setting mode since I want the time setting mode to essentially do the same thing. I didn't show the other functions because they work properly. It's just this function I'm having trouble with. Any help would be greatly appreciated

void loop ()
{
  now = rtc.now();
  lcd.clear();
  displayTime();
  modeState = digitalRead(modeSetPin);
  incHoursState = digitalRead(incHoursPin);
  incMinsState = digitalRead(incMinsPin);
  setMode();
}

void setMode()
{
  if (modeState == LOW)
  {
    Serial.println("Test 1");
    Serial.println(modeButtonCount);
    
    if (modeButtonCount > 1)// Mode button held for 2s
    {
      if (digitalRead(setTimeAlarmSw) == HIGH)
      {
        //Setting Alarm
        Serial.println("Test 2");
        lcd.clear();
        lcd.home();
        lcd.print("Snooze to Set");
        lcd.setCursor(0, 1);
        lcd.print("Alarm = ");

        if (incHoursState == LOW)
        {
          Serial.print("aHr: ");
          Serial.println(aHr);
          aHr++;
          if (aHr > 23)
          {
            aHr = 0;
            aMins = 0;
            aSecs = 0;
          }
        }

        if (incMinsState == LOW)
        {
          Serial.print("aMins: ");
          Serial.println(aMins);
          aMins++;
          if (aMins > 59)
          {
            aMins = 0;
          }
        }

        displayAlarm();
        delay(3000);
        modeButtonCount = 0;
      }
      //Setting Time
      else
      {
        lcd.clear();
        lcd.home();
        lcd.print("Snooze to Set");
        lcd.setCursor(0, 1);
        lcd.print("Time = ");
        //setDateTime();
        delay(3000);
        modeButtonCount = 0;
      }
    }
    else
    {
      modeButtonCount++;
      Serial.println(modeButtonCount);
    }
  }
}

    if (modeButtonCount > 1)// Mode button held for 2sI can't see how you know that the button has been held for more than 2 seconds. What have I missed ?

At the end of my displayTime function there's a delay for 1s. It takes 1 second for the time to update so I just used that

void displayTime()
{
  int mins = now.minute();
  int secs = now.second();
  int hr = now.hour();
  int displayHr = hr;
  int dayofweek = now.dayOfWeek();

  if (displayHr > 12)
  {
    displayHr = displayHr - 12;
    lcd.print(displayHr);
  }
  else if (displayHr == 0)
  {
    lcd.print("12");
  }
  else
  {
    lcd.print(displayHr);
  }
  lcd.print(":");
  if (mins < 10)
  {
    lcd.print(0);
  }
  lcd.print(mins);
  lcd.print(":");
  if (secs < 10)
  {
    lcd.print(0);
  }
  lcd.print(secs);
  lcd.print(" ");

  if (hr < 12)
  {
    lcd.print("AM");
  }
  else
  {
    lcd.print("PM");
  }

  lcd.setCursor(0, 1);
  lcd.print(days[now.dayOfWeek()]);
  lcd.print(", ");
  lcd.print(months[now.month()]);
  lcd.print(" ");
  lcd.print(now.day());
  lcd.print(" ");
  lcd.print(now.year());
  delay(1000);
}

I don't know what is causing your current problem, but the delay()s scattered through your program are going to cause problems at some stage I suspect. Why do you need to delay() 1 second, or delay() at all in the function that displays the time ? Surely it would be better to display the time when it changes which would make the system much more responsive to inputs.

To avoid further confusion can I suggest that you post your whole program and not just bits of it.

At the end of my displayTime function there's a delay for 1s. It takes 1 second for the time to update so I just used that

And we were supposed to know that...how? This is why is makes little to no sense to post a code fragment. Nick Gammon maintains a site (SnippetsAreUs.com) for posting code snippets, but not here.

I made the delays so I could follow what was going on through the Serial monitor. I was going to replace some of them with millis() so they don't stop the rest of my program but I just wanted some quick debugging. Should I do that instead?

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DateTime now;

const int modeSetPin = 9;   // Used to enter time or alarm configuration settings
const int incHoursPin = 7; // Used to increment the alarm hours in alarm set mode
const int incMinsPin = 8; // Used to increment the alarm minutes in alarm set mode
const int setTimeAlarmSw = 6; //Used to switch between time set and alarm set modes

//this section initializes the button counters, which is how the buttons can have multiple operations within the same function
int modeState = 0;
int lastModeState = 0;
int modeButtonCount = 0;
int incHoursState = 0;
int incMinsState = 0;

int aHr = 0;
int aMins = 0;
int aSecs = 0;

char months[][12] = {"No", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char days[][7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "No"};

void setup () {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  lcd.begin(16, 2);
  lcd.print("UNO Alarm Clock");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.println("Made by Kidd17");
  delay(1000);

  pinMode(modeSetPin, INPUT);
  pinMode(incHoursPin, INPUT);
  pinMode(incMinsPin, INPUT);
  pinMode(setTimeAlarmSw, INPUT);

  //rtc.adjust(DateTime((__DATE__), (__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

void loop ()
{
  now = rtc.now();
  lcd.clear();
  displayTime();
  modeState = digitalRead(modeSetPin);
  incHoursState = digitalRead(incHoursPin);
  incMinsState = digitalRead(incMinsPin);
  setMode();
}

void displayTime()
{
  int mins = now.minute();
  int secs = now.second();
  int hr = now.hour();
  int displayHr = hr;
  int dayofweek = now.dayOfWeek();

  if (displayHr > 12)
  {
    displayHr = displayHr - 12;
    lcd.print(displayHr);
  }
  else if (displayHr == 0)
  {
    lcd.print("12");
  }
  else
  {
    lcd.print(displayHr);
  }
  lcd.print(":");
  if (mins < 10)
  {
    lcd.print(0);
  }
  lcd.print(mins);
  lcd.print(":");
  if (secs < 10)
  {
    lcd.print(0);
  }
  lcd.print(secs);
  lcd.print(" ");

  if (hr < 12)
  {
    lcd.print("AM");
  }
  else
  {
    lcd.print("PM");
  }

  lcd.setCursor(0, 1);
  lcd.print(days[now.dayOfWeek()]);
  lcd.print(", ");
  lcd.print(months[now.month()]);
  lcd.print(" ");
  lcd.print(now.day());
  lcd.print(" ");
  lcd.print(now.year());
  delay(1000);
}

void setMode()
{
  if (modeState == LOW)
  {
    Serial.println("Test 1");
    Serial.println(modeButtonCount);
    
    if (modeButtonCount > 1)// Mode button held for 2s
    {
      if (digitalRead(setTimeAlarmSw) == HIGH)
      {
        //Setting Alarm
        Serial.println("Test 2");
        
        lcd.clear();
        lcd.home();
        lcd.print("Snooze to Set");
        lcd.setCursor(0, 1);
        lcd.print("Alarm = ");

        if (incHoursState == LOW)
        {
          Serial.print("aHr: ");
          Serial.println(aHr);
          aHr++;
          if (aHr > 23)
          {
            aHr = 0;
            aMins = 0;
            aSecs = 0;
          }
        }

        if (incMinsState == LOW)
        {
          Serial.print("aMins: ");
          Serial.println(aMins);
          aMins++;
          if (aMins > 59)
          {
            aMins = 0;
          }
        }

        displayAlarm();
        delay(3000);
        modeButtonCount = 0;
      }
      //Setting Time
      else
      {
        lcd.clear();
        lcd.home();
        lcd.print("Snooze to Set");
        lcd.setCursor(0, 1);
        lcd.print("Time = ");
        delay(3000);
        modeButtonCount = 0;
      }
    }
    else
    {
      modeButtonCount++;
      Serial.println(modeButtonCount);
    }
  }
}

void displayAlarm()
{
  Serial.println("Test displayAlarm();");
  if (aHr < 10)
  {
    lcd.print(0);
    lcd.print(aHr);
  }
  else
  {
    lcd.print(aHr);
  }
  lcd.print(":");

  if (aMins < 10)
  {
    lcd.print(0);
  }
  lcd.print(aMins);
  lcd.print(":");

  if (aSecs < 10)
  {
    lcd.print(0);
  }
  lcd.print(aSecs);
  lcd.print(" ");
}

First, in several places you have code similar to:

  if (mins < 10)
  {
    lcd.print(0);
  }

The intent is to display a leading zero before the digit character. A more consistent way to document your intent is to use:

  if (mins < 10)
  {
    lcd.print('0');
  }

since you want to display a char, not an int.

Also, you might take a look at the Timekeeper Register, page 8, in:

http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

Hi, I've arrived late to the party. Did OP or anyone else solved how to increment hours and minutes on the RTC?