Help with timer code not working

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

First, your code will be easier to read if you format it using conventional C style. You can have the IDE do this for you by placing the cursor in the source code window and pressing Ctrl-T which will auto-format your code.

Next, the statement:

  if(msec=(1000+msec_inc;))  {seconds++; msec=0;}

prevents compilation because of a syntax error. I'm not sure, but I'm guessing you want to up the seconds count as in:

if (msec_inc % 1000 == 0) 
{
  seconds++; 
  msec=0;
}

Also, your code appears to assume that each pass through loop() takes one millisecond since you're incrementing msec on each pass. This is likely not the case. Instead, you should call the millis() function which maintains a count for you of the number of milliseconds that have passed since the program began execution. (it rolls over every 50 days.) Also, you will have to initialize your clock data in setup() to whatever values prevail when the program starts.

If you really want a clock, I'd consider a Real Time Clock (RTC) shield, as most have 20 ppm accuracy and cost less a couple of dollars.

if(msec=(1000+msec_inc;))  {seconds++; msec=0;}

should be:

if(msec==(1000+msec_inc;))  {seconds++; msec=0;}

should be: Code:

if(msec==(1000+msec_inc;))

No, I really don't think so.

if(msec==(1000+msec_inc))  {seconds++; msec=0;}

Then...