Back to the Future Clock

Greetings,

I am a novice when it comes to coding. I have recently made a Back to the Future clock and need some help with the coding for the current time. I will provide a link to the YouTube channel where I got this idea from. The code is on Thingiverse, but will provide it here so you can take a look. The issue is that it seems that the time zone is not being set correctly. The minutes are correct, but the hours are ahead. I forgot what the total was, and I apologize for that. That is why I am thinking it is a time zone issue. I currently live in the Central time zone in the United States. Any help would be appreciated.

YouTube: Back to the Future clock - WIFI EDITION [ENG SUB] - YouTube

Code:

#include "Arduino.h"
#include "TM1637Display.h"
#include "ArduinoJson.h"
#include "WiFiManager.h"
#include "NTPClient.h"

// Module connection pins (Digital Pins)
#define red_CLK 13
#define red1_DIO 14
#define red2_DIO 15
#define red3_DIO 16

#define green_CLK 17
#define green1_DIO 18
#define green2_DIO 19
#define green3_DIO 21

#define orange_CLK 22
#define orange1_DIO 23
#define orange2_DIO 25
#define orange3_DIO 26

#define greenAM 32
#define greenPM 33

bool res;
int Hour = 0;
//========================USEFUL VARIABLES=============================
int UTC = 2; // UTC + value in hour - Summer time
const long utcOffsetInSeconds = 3600; // Offset in second
int Display_backlight = 255;

//Set the red displays
int red_day = 26;
int red_month = 10;
int red_year = 1985;
int red_hour = 9;
int red_minute = 00;

//set the orange displays
int orange_day = 12;
int orange_month = 11;
int orange_year = 1955;
int orange_hour = 06;
int orange_minute = 38;

//=====================================================================

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds*UTC);

TM1637Display green1(green_CLK, green1_DIO);
TM1637Display green2(green_CLK, green2_DIO);
TM1637Display green3(green_CLK, green3_DIO);

TM1637Display red1(red_CLK, red1_DIO);
TM1637Display red2(red_CLK, red2_DIO);
TM1637Display red3(red_CLK, red3_DIO);

TM1637Display orange1(orange_CLK, orange1_DIO);
TM1637Display orange2(orange_CLK, orange2_DIO);
TM1637Display orange3(orange_CLK, orange3_DIO);

void setup()
{
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
Serial.begin(115200);

WiFiManager manager;    

manager.setTimeout(180);
//fetches ssid and password and tries to connect, if connections succeeds it starts an access point with the name called "BTTF_CLOCK" and waits in a blocking loop for configuration
res = manager.autoConnect("BTTF_CLOCK","password");

if(!res) {
Serial.println("failed to connect and timeout occurred");
ESP.restart(); //reset and try again
}

timeClient.begin();
red1.setBrightness(Display_backlight);
red2.setBrightness(Display_backlight);
red3.setBrightness(Display_backlight);

green1.setBrightness(Display_backlight);
green2.setBrightness(Display_backlight);
green3.setBrightness(Display_backlight);

orange1.setBrightness(Display_backlight);
orange2.setBrightness(Display_backlight);
orange3.setBrightness(Display_backlight);

}

void loop()
{
timeClient.update();
Serial.print("Time: ");
Serial.println(timeClient.getFormattedTime());
unsigned long epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime ((time_t *)&epochTime);
int currentYear = ptm->tm_year+1900;
Serial.print("Year: ");
Serial.println(currentYear);

int monthDay = ptm->tm_mday;
Serial.print("Month day: ");
Serial.println(monthDay);

int currentMonth = ptm->tm_mon+1;
Serial.print("Month: ");
Serial.println(currentMonth);

  if (timeClient.getHours() == 0)

{
Hour = 12;
digitalWrite(greenAM, 1);
digitalWrite(greenPM, 0);
Serial.write(" =0");
}

else if (timeClient.getHours() == 12)
{
Hour = timeClient.getHours();
digitalWrite(greenAM, 0);
digitalWrite(greenPM, 1);
Serial.write(" =12");
}

else if (timeClient.getHours() >= 13) {
Hour = timeClient.getHours() - 12;
digitalWrite(greenAM, 0);
digitalWrite(greenPM, 1);
Serial.write(" >=13");
}

else {
Hour = timeClient.getHours();
digitalWrite(greenAM, 1);
digitalWrite(greenPM, 0);
Serial.write("else");
}

red1.showNumberDecEx(red_month,0b01000000,true,2,0);
red1.showNumberDecEx(red_day,0b01000000,true,2,2);
red2.showNumberDecEx(red_year,0b00000000,true);
red3.showNumberDecEx(red_hour,0b01000000,true,2,0);
red3.showNumberDecEx(red_minute,0b01000000,true,2,2);

green1.showNumberDecEx(currentMonth,0b01000000,true,2,0);
green1.showNumberDecEx(monthDay,0b01000000,true,2,2);
green2.showNumberDecEx(currentYear,0b00000000,true);
green3.showNumberDecEx(Hour,0b01000000,true,2,0);
green3.showNumberDecEx(timeClient.getMinutes(),0b01000000,true,2,2);

orange1.showNumberDecEx(orange_month,0b01000000,true,2,0);
orange1.showNumberDecEx(orange_day,0b01000000,true,2,2);
orange2.showNumberDecEx(orange_year,0b00000000,true);
orange3.showNumberDecEx(orange_hour,0b01000000,true,2,0);
orange3.showNumberDecEx(orange_minute,0b01000000,true,2,2);

if((currentMonth30 + monthDay) >= 121 && (currentMonth30 + monthDay) < 331){
timeClient.setTimeOffset(utcOffsetInSecondsUTC);} // Change daylight saving time - Summer
else {timeClient.setTimeOffset((utcOffsetInSeconds
UTC) - 3600);} // Change daylight saving time - Winter

if(timeClient.getHours()>=13){
digitalWrite(greenAM,0);
digitalWrite(greenPM,1);}

else if(timeClient.getHours()==12){
digitalWrite(greenAM,0);
digitalWrite(greenPM,1);}

else{
digitalWrite(greenAM,1);
digitalWrite(greenPM,0);}

delay(20);

}

Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum when its description is

:warning: DO NOT CREATE TOPICS IN THIS CATEGORY :warning:

Your topic has been moved to the Programming category

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

Ok, so then why isn't that category removed from the drop down menu. My apologies.

Apology accepted

The category is part of an experiment being carried with regard to restructuring the forum categories

Having posted in it you are now part of the results of the experiment

will be accepted when you add the code tags too :slight_smile:

➜ Edit your post using the :pencil2: in the tool bar just below your post

select the code part and press the <code/> icon in the tool bar to mark it as code.

(also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

➜ do yourself a favour and please read How to get the best out of this forum

After you catch up with the forum guide, don't forget to tell us which Arduino you are using.

You are going to have to change the value of the variable "UTC" from 2 to -6 (?) in the snippet below:

//========================USEFUL VARIABLES=============================
int UTC = 2; // UTC + value in hour - Summer time
const long utcOffsetInSeconds = 3600; // Offset in second

"UTC" is the offset from UTC time in hours, it is used in the following:

//=====================================================================

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds*UTC);

I think you will also need to make an adjustment to the part of the code that changes to/from daylight saving time, as the US makes the change on a different day to Europe.

if((currentMonth30 + monthDay) >= 121 && (currentMonth30 + monthDay) < 331){
timeClient.setTimeOffset(utcOffsetInSecondsUTC);} // Change daylight saving time - Summer
else {timeClient.setTimeOffset((utcOffsetInSecondsUTC) - 3600);} // Change daylight saving time - Winter

It is the values 121 and 331 that will need to be changed to ??? and ???

Are you using an ESP? If so, there are built in functions to manage time zone and DST offsets. You need not struggle with frail, elaborate code if you do it the built in system way. Look at the "SimpleTime" example sketch.

Ugh....metric system and now this. Thanks I will try these things out.

Yes, esp32.

ESP32 is especially well "tricked out" for timekeeping. Again, there is no good reason to re-invent and possibly fail at timekeeping routines when they are fully and mostly transparently handled by the core. That applies both to NTP synchronization and time zone management.

Ugh....metric system

You mean the global measurement system that has been adopted by every country in the world except for one?

1 Like

First post still ugly… I guess apologies were just fake…
I’m out.