Close

The Universal Time Clock

A project log for Polling a Push Button Rotary Encoder

A push button rotary encoder is a neat input device for an Arduino. Unfortunately interfacing to it is not!

agp.cooperagp.cooper 01/30/2018 at 07:160 Comments

Universal Time

Universal Time (UT) is the time in Greenwich UK. It is used for astromonical observations.

This is a simple real time clock (rtc) project that uses and LCD to display the date and time in UT.

Setting the time is automatic, it uses the compile time set local time and then reset that time to UT based on an upload adjustment and local time adjustments. 

In my case the upload tme is 7 seconds, and my local time adjustment is -8 hours and 0 minutes. I live in Perth Western Australia and the longitude is 115.9 degrees East or 115.9 degrees. The local adjustment to the nearest hour is =-int(long/15+0.5), or -8 hours. Unfortunately many places have 30 minute local adjstments as well.

Here is the code:

// Include RTC libraries and initialise
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;

// Include LCD library and initialise
#include <LiquidCrystal.h>
// Define Adruino pin numbers for LCD
#define en   7
#define rs   8
#define d4   9
#define d5  10
#define d6  11
#define d7  12
#define led 13
// Set LCD
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
  // Turn on LCD LED
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH);
  
  // Setup LCD columns and rows
  lcd.begin(16, 2);
  
  // Print a message to the LCD.
  lcd.setCursor(0,0);lcd.print("Universal Time");
  // Check RTC
  if (!rtc.begin()) {
    lcd.setCursor(0,1);lcd.print("RTC not found");
    while (true);
  } else if (!rtc.isrunning()) {
    lcd.setCursor(0,1);lcd.print("RTC not running");
  } else {
    lcd.setCursor(0,1);lcd.print("RTC found");
  }
  
  // Set Universal Time based on Compile Time
  int localTimeHour=-8; // Perth Western Australia
  int localTimeMin=0;   // Perth Western Australia
  int uploadTimeSec=7;  // For my computer

  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  DateTime now=rtc.now();
  rtc.adjust(DateTime(now+TimeSpan(0,localTimeHour,localTimeMin,uploadTimeSec)));
  
  delay(1000);
  lcd.clear();
}

void loop() {
  
  DateTime now=rtc.now();

  // Print date on first line of LCD
  lcd.setCursor(0,0);
    lcd.print(now.year());
    lcd.print('/');
    if (now.month()<10) lcd.print(0);
    lcd.print(now.month());
    lcd.print('/');
    if (now.day()<10) lcd.print(0);
    lcd.print(now.day());
    
  // Print time on second line of LCD
  lcd.setCursor(0,1);lcd.print("UT: ");
    if (now.hour()<10) lcd.print(0);
    lcd.print(now.hour());
    lcd.print(':');
    if (now.minute()<10) lcd.print(0);
    lcd.print(now.minute());
    lcd.print(':');
    if (now.second()<10) lcd.print(0);
    lcd.print(now.second());
    
  delay(1000);
}

The only tricky bit here was setting UT after setting local time:

  // Set Universal Time based on Compile Time
  int localTimeHour=-8; // Perth Western Australia
  int localTimeMin=0;   // Perth Western Australia
  int uploadTimeSec=7;  // For my computer

  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  DateTime now=rtc.now();
  rtc.adjust(DateTime(now+TimeSpan(0,localTimeHour,localTimeMin,uploadTimeSec)));

This line sets the rtc to compile time:

rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

This line get the current time (now):

DateTime now=rtc.now();

This line set UT based on an upload adjustment and local time adjustments:

rtc.adjust(
  DateTime(
    now+TimeSpan(
      0,localTimeHour,localTimeMin,uploadTimeSec
    )
  )
);


Magic

 

Discussions