I'm just getting started with Arduino. I'm trying to make a dual clock for my ham shack with an RTC and 2 line display. I downloaded this code and tweaked it to try to get the clock working. I figured out most of it but there's something weird going on with the math.
If I set the local time to 11:00:00 and the offset (line 51) to +12, the time shows 23:00:00 as it should. However, if I set the offset to +13 (Just to pass the 24 hour mark), the time jumps to 02:00:00. It should show 00:00:00 (midnight the next morning). If I change it to +14 it jumps to 12:00:00. +15 = 22:00:00, etc.
Thanks for any help.
#include "Wire.h"
#define DS1307_ADDRESS 0x68
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
void setup(){
Wire.begin();
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
}
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
int Zulu=((hour)+13); //CHANGE FOR DST
//START SERIAL
// Serial.print(Local); THIS WORKS
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
if((minute) < 10)
{
Serial.print("0");
}
Serial.print(minute);
Serial.print(":");
if((second) < 10.0)
{
Serial.print("0");
}
Serial.println(second);
//PRINT LOCAL TO LCD
// lcd.setCursor(0,0);
// lcd.print(" "); This just makes the display flicker
lcd.setCursor(0,0);
if((hour) < 10)
{
lcd.print("0");
}
lcd.print(hour);
lcd.setCursor(2,0);
lcd.print(":");
lcd.setCursor(3,0);
if((minute) < 10)
{
lcd.print("0");
}
lcd.print(minute);
lcd.setCursor(5,0);
lcd.print(":");
lcd.setCursor(6,0);
if((second) < 10.0)
{
lcd.print("0");
}
lcd.print(second);
lcd.setCursor(9,0);
lcd.print("Phelan");
//END LOCAL
//PRINT TO LCD ZULU SET IN VAR for CALC's
// lcd.setCursor(0,1);
// lcd.print(" ");
lcd.setCursor(1,1);
if((Zulu) > 23.00) // Adjusting for UTC
{
lcd.print((Zulu)-24);
}
lcd.print(Zulu);
lcd.setCursor(3,1);
lcd.print(":");
lcd.setCursor(4,1);
if((minute) < 10)
{
lcd.print("0");
}
lcd.print(minute);
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(7,1);
if((second) < 10.0)
{
lcd.print("0");
}
lcd.print(second);
lcd.setCursor(10,1);
lcd.print("Zulu");
//END ZULU
}