# Help with timer code not working

**URL:** <https://forum.arduino.cc/t/help-with-timer-code-not-working/177823>\
**Category:** Programming\
**Created:** [August 11, 2013, 5:10pm UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823 "2013-08-11T17:10:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 11, 2013, 5:10pm UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/1 "2013-08-11T17:10:25Z")

</div>

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

```cpp
#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

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [August 11, 2013, 5:49pm UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/2 "2013-08-11T17:49:38Z")

</div>

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:

```cpp
  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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 12, 2013, 10:39am UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/3 "2013-08-12T10:39:36Z")

</div>

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

```

should be:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 12, 2013, 10:46am UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/4 "2013-08-12T10:46:24Z")

</div>

> should be:  
> Code:
> 
> if(msec==(1000+msec\_inc;))

No, I really don't think so.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 12, 2013, 9:07pm UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/5 "2013-08-12T21:07:05Z")

</div>

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

```

Then...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:18pm UTC](https://forum.arduino.cc/t/help-with-timer-code-not-working/177823/6 "2021-05-05T20:18:38Z")

</div>


