[HELP] LED arduino 2650 clock using RTC

Hi there! I'm almost finished with this code for my LED clock, but I'm running into an issue attempting to use buttons to set the time. I can "set" it, but the changes don't kick back in unless I reopen the serial monitor. As in, if I turn on the "set time" button, then add five hours and turn off the "set time" button, changes don't reflect until I open serial monitor. I want the changes to reflect when the "set time" button is released.

#include <Wire.h> // specify use of Wire.h library.
#include <Time.h>
#include <DS1307RTC.h>


#define ONE_HZ_SW 1 // one Hz square wave from Ds1307
#define blinkPin 13
#define Sw0 4
#define Sw1 5
#define Sw2 6



int hrPins[] = {52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30};
int minPins[] = {31, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 33};

void setup()
{
  tmElements_t tm;

  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
  digitalWrite(blinkPin, 0);


  pinMode(ONE_HZ_SW, INPUT_PULLUP);
  pinMode(Sw0, INPUT_PULLUP);  // for this use a slide switch
  pinMode(Sw1, INPUT_PULLUP);  // N.O. push button switch
  pinMode(Sw2, INPUT_PULLUP);  // N.O. push button switch

  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop()
{

  tmElements_t tm;

  // wait or HIGH
  while (!digitalRead(ONE_HZ_SW))
  {
  }
  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  }
  else
  {
    if (RTC.chipPresent())
    {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    }
    else
    {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);

tm.Hour = (tm.Hour + 11) % 12; // This maps 0 to 11 (all on), 1 to 0 (only first on), 11 to 10 (all but last on), 12 to 11 (all on), 13 to 0 (first on), 23 to 10 (all but last on)

for (int i = 0; i < 12; i++)
  {
    digitalWrite(hrPins[i], tm.Hour >= i);
    tm.Minute /= 5; // We only care about the time in 5-minute increments
  }
for (int i = 1; i < 12; i++)
 { 
  digitalWrite(minPins[i], tm.Minute >= i);
  digitalWrite(minPins[0], tm.Minute == 0); // The first pin behaves differently: it only turns on during the first 5 minutes
 }

  if (!(digitalRead(Sw0))) set_time(); // hold the switch to set time
    while (digitalRead(ONE_HZ_SW))
{
  }// wait for low

  toggle(blinkPin);

}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

// toggle the state on a pin
void toggle(int pinNum)
{
  int pinState = digitalRead(pinNum);
  pinState = !pinState;
  digitalWrite(pinNum, pinState);
}

void set_time() {
    byte minutes = 0;
    byte hours = 0;

    while (!digitalRead(Sw0))  // set time switch must be released to exit
    {
        while (!digitalRead(Sw1)) // set minutes
        { 
            minutes++;          
            if ((minutes & 0x0f) > 9) minutes = minutes + 6;
            if (minutes > 0x59) minutes = 0;
            Serial.print("Minutes = ");
            if (minutes >= 9) Serial.print("0");
            Serial.println(minutes, HEX);

            delay(250);
        }

        while (!digitalRead(Sw2)) // set hours
        { 
            hours++;          
            if ((hours & 0x0f) > 9) hours =  hours + 6;
            if (hours > 0x23) hours = 0;
            Serial.print("Hours = ");
            if (hours <= 9) Serial.print("0");
            Serial.println(hours, HEX);

            delay(250);
        }

   Wire.beginTransmission(0x68); // activate DS1307
    Wire.write(0); // where to begin
    Wire.write(0x00);          //seconds
    Wire.write(minutes);          //minutes
    Wire.write(0x80 | hours);    //hours (24hr time)
    Wire.write(0x06);  // Day 01-07
    Wire.write(0x16);  // Date 0-31
    Wire.write(0x11);  // month 0-12
    Wire.write(0x13);  // Year 00-99
    Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. 
    Wire.endTransmission();
}
}
  while (!digitalRead(ONE_HZ_SW))[color=#222222][/color]
  {[color=#222222][/color]
  }

The above code might be freezing your loop. When you open serial monitor, the arduino will reset.

I tried deleting that line, but I'm still not getting the code to loop. I'm not sure what's getting in the way :confused:

You have 90 minutes in an hour on your clock?

if (minutes > 0x59) minutes = 0;

and 36 hours in a day

if (hours > 0x23) hours = 0;

Why do you not use the RTC.set/RTC.write instead of your own code?
At least it should be move out of the while (!digitalRead(Sw0)) loop so the time only gets written back after the set time switch is released.

This is how I've had the set-time configured from the beginning, using code found at

It actually does work, but the problem is I need to start the serial monitor every time. I think something is jamming up my code but I can't figure out what it is

IanJM:
This is how I've had the set-time configured from the beginning, using code found at

Arduino Interfacing DS1307 Real Time Clock

It actually does work, but the problem is I need to start the serial monitor every time. I think something is jamming up my code but I can't figure out what it is

It just seems weird you would install and use the DS1307RTC library for reading the time but then write your own code to set it instead of using the libraries own time setting code.

You have not commented on why your using hexadecimal for your hours and minutes though I suspect it's because it's easier to convert to BCD

Maybe when your setting the new time in the RTC your corrupting it somehow and causing the 1Hz signal to stop so the below while loop never exits. Then when you open/close the serial monitor your resetting the Arduino and enabling the 1Hz signal again.

  if (!(digitalRead(Sw0))) set_time(); // hold the switch to set time
  while (digitalRead(ONE_HZ_SW))
  {
  }// wait for low

Try commenting out the while loop and/or don't write to the control register when setting the time and see if this works.