Hi all,
I have been trying to recreate a project for my nephew as a present with succes (or sort of). It looks like a stack of dynamite, however, it is in fact just digital clock.
My only problem is that I live in Denmark and, thus, the code provided by OP doest not take daylight saving time into account. The clock is now an hour in advance. I have tried to include the DST automatically adjusting RTC time for Daylight Saving Time (DST) in the code below, however, with no luck.
Is there by any chance somebody who can help with a solution
KR Fred
Original code:
// include the library code:
// corrected seconds>10 problem. no idea how old file got in. 010320
#include <Wire.h> // serial 12c library
#include <RTClib.h> // rtc library
#include "LedControl.h"
LedControl lc = LedControl (6, 5, 4, 1); // LED module pins plus number of displays (DATA, CLK, LOAD, DISP NO.)
RTC_DS3231 rtc; // rtc chip
// -------------------------------------------------------------------------
// DECLARE VARIABLES ETC
int theyear = 0;
int tmonth = 0;
int dom = 0;
int hours = 0; // time / date global variables
int mins = 0;
int dow = 0;
int secs = 0;
int led = 0; // run led state
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // days text array
char status; // pressure variable
//SETUP-------------------------------------------------------------------------
void setup() {
//Serial.begin(9600);
rtc.begin(); // clock init
// *** use this next line to set clock to PC time.
//rtc.adjust(DateTime(F(DATE), F(TIME))); // set time to PC compile date
// set ddrs
pinMode(2,OUTPUT); // run LED
pinMode(4,OUTPUT); // data
pinMode(5,OUTPUT); // clock
pinMode(6,OUTPUT); // load
pinMode(8,OUTPUT); // buzzer
pinMode(LED_BUILTIN, OUTPUT); // on board
// initialise leds----------------------------------------------------------------------------
int devices = lc.getDeviceCount(); // initialise LED modules loop
for (int address = 0; address < devices; address++) {
lc.shutdown(address, false); // Wake up LED chips
lc.setIntensity(address, 15); // Brightness
lc.clearDisplay(address); // Clear Displays
}
}
//--------display hour ----------------------------------------------------------------------------------------------------
void displayhr() {
String paramSt = String(); // String declared
byte ledchar[ ] = {0, 0, 0, 0, 0, 0, 0, 0}; // Byte array from string
int j = 0;
paramSt = hours; // Int into string
for ( j = 0 ; j < 7 ; j++ ) ledchar[j] = paramSt[j] - 48; // char to byte conversion and ascii offset
if ( hours < 10) {
lc.setDigit(0, 6, ledchar[0], false);
lc.setDigit(0, 7, 0 , false);
}
else if ( hours >= 10) {
lc.setDigit(0, 6, ledchar[1], false);
lc.setDigit(0, 7, ledchar[0], false);
}
}
//----------display minute --------------------------------------------------------------------------------------------------
void displaymins() {
String paramSt = String(); // String declared
byte ledchar[ ] = {0, 0, 0, 0, 0, 0, 0, 0}; // Byte array from string
int j = 0;
paramSt = mins; // Int into string
for ( j = 0 ; j < 7 ; j++ ) ledchar[j] = paramSt[j] - 48; // char to byte conversion and ascii offset
if ( mins < 10) {
lc.setDigit(0, 3, ledchar[0], false);
lc.setDigit(0, 4, 0 , false);
}
else if ( mins >= 10) {
lc.setDigit(0, 3, ledchar[1], false);
lc.setDigit(0, 4, ledchar[0], false);
}
}
//--------------display seconds -----------------------------------------------------------------------------------------------
void displaysecs() {
String paramSt = String(); // String declared
byte ledchar[ ] = {0, 0, 0, 0, 0, 0, 0, 0}; // Byte array from string
int j = 0;
paramSt = secs; // Int into string
for ( j = 0 ; j < 7 ; j++ ) ledchar[j] = paramSt[j] - 48; // char to byte conversion and ascii offset
if ( secs < 10) {
lc.setDigit(0, 0, ledchar[0], false);
lc.setDigit(0, 1, 0 , false);
}
else if ( secs >= 10) {
lc.setDigit(0, 0, ledchar[1], false);
lc.setDigit(0, 1, ledchar[0], false);
}
}
//-RUN LED / flashing cursors ----------------------------------------------------------------------------------------------
void runled(){
if (led == 0){
digitalWrite(2, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
lc.setRow(0, 5, B00000001); // dots between digits
lc.setRow(0, 2, B00000001); // dots between digits
led = 1;
return;
}
if (led == 1){
digitalWrite(2, LOW);
digitalWrite(LED_BUILTIN, LOW);
lc.setRow(0, 5, B00000000); // dots between digits
lc.setRow(0, 2, B00000000); // dots between digits
led = 0;
}
}
// MAIN PROGRAM----------------------------------------------------------------------------------------------
void loop() {
DateTime now = rtc.now();
theyear = now.year();
tmonth = now.month();
dom = now.day(); //Move RTC variables to global variables.
hours = now.hour();
mins = now.minute();
secs = now.second();
dow = now.dayOfTheWeek();
delay(495);
digitalWrite(8, HIGH);
delay(5);
digitalWrite(8, LOW);
displayhr(); // display hour
displaymins(); // display min
if(led == 1)displaysecs(); // display secs only on cursor drop not cursor light. avoids occasional double update
runled(); // flash seperators
//Serial.println(secs);
}
//---------------------------------------------------------------------------------------------------------------------------------------
