Hello Community,
I am trying to make a clock using an Arduino UNO Rev 3 and a serial 16x2 LCD.
I am having some issues with getting the code to work right, though I'm sure it is a coding error, I just cant find the error.
I have also tried different ways to achieve my goal, but no success.
here is my code
#include <SoftwareSerial.h>
#include <Math.h>
int msec=0;
int weeks=0;
int days=0;
int hours=0;
int minutes=0;
int seconds=0;
int msec_inc=0;
int inc=0;
void setup()
{Serial.begin(9600); delay(500);} //setup
void loop()
{
dispTimer(msec);
delay(1); msec++; //Increment each milisecond
}
void dispTimer(int msec)
{
//Increment all clocks
if(msec=(1000+msec_inc;)) {seconds++; msec=0;}
if(seconds==60) {minutes++; seconds=0;}
if(minutes==60) {hours++; minutes=0;}
if(hours==24) {days++; hours=0;}
if(days==7) {weeks++; days=0;}
//Display Elapsed Time and output formastting
Serial.print(" ");
Serial.print(weeks);
Serial.print("w ");
Serial.print(days);
Serial.print("d ");
if(hours<10) {Serial.print("0"); Serial.print(hours);}
else {Serial.print(hours);}
Serial.print(":");
if(minutes<10) {Serial.print("0"); Serial.print(minutes);}
else {Serial.print(minutes);}
Serial.print(":");
if(seconds<10) { Serial.print("0"); Serial.print( seconds);}
else {Serial.print(seconds);}
Serial.print (" ");
}
It should be pretty simple, but the seconds are counting up with the speed of milliseconds.
What am I doing wrong? Is there a more short and sweet way to code this?
The reason I am using milliseconds and msec_inc variable is to try to adjust for the Arduino clock drift....Initially I found a drift of about 10 seconds/10 hours on to of 1 second/10 minutes, but I wanted a more precise adjustment as I would like to achieve an accuracy of +/- 15 min over 1 week.
Is there a cleaner way to achieve this?
Any other advice to clean up my coding, as it has been a long time since I coded in C or C++.
Many thanks in advance.
wc