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();
}
}