clock with millis

Hi,

I am using millis for 16x2 LCD clock project. it is counting seconds, minutes and hours. it is starting 00:00:00 (hh:mm:ss). and after when the time 09:06:07, LCD stop. I tried 3 times and it stop 09:06:07. LCD screen is freezing but arduino LED is blinking. I did not look serial port screen.

Why does it stop?. I did not wanted to use RTC module. is it impossible to make a clock with 24hours clock? if it is impossible, I can use RTC-module, but firstly I want to learn, why it is stop at the same time.

I am using Arduino-nano-Atmega328P and 1602 LCD Display

MY codes are bottom.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd {0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE};

int starttime;
int activetime;
int prevoustime = 0;

int hours = 0;
int mins = 0;
int secs = 0;

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();

  Serial.begin(9600);
  starttime = millis() / 1000;
}

void loop()
{

  activetime = (millis() / 1000) - starttime;
  if (prevoustime < activetime)
  {
    secs++;
    prevoustime = activetime;
  }


  if (secs > 59)
  {
    mins++;
    secs = 0;
  }

  if (mins > 59)
  {
    hours++;
    mins = 0;
  }

  if (hours > 23)
  {
    hours = 0;
  }


  lcd.setCursor(0, 0);

  if (hours < 10)
  {
    lcd.print("0");
    lcd.print(hours);
  }
  else
  {
    lcd.print(hours);
  }

  lcd.print(":");

  if (mins < 10)
  {
    lcd.print("0");
    lcd.print(mins);
  }
  else
  {
    lcd.print(mins);
  }

  lcd.print(":");
  if (secs < 10)
  {
    lcd.print("0");
    lcd.print(secs);
  }
  else
  {
    lcd.print(secs);
  }
  delay(100);
  lcd.clear();


  Serial.println(mins);
  Serial.println(hours);
  Serial.println("");
  Serial.println("");
  Serial.println(activetime);
  Serial.println(prevoustime);
  Serial.println(starttime);
  Serial.println("");
}

An int stores values in the range -32768 to +32767

... and guess how many seconds there are in 09:06:07?

TheMemberFormerlyKnownAsAWOL:
An int stores values in the range -32768 to +32767

TheMemberFormerlyKnownAsAWOL:
An int stores values in the range -32768 to +32767

thank you for help. 32767 seconds later it stop. Ok. I understood. I deleted seconds counter codes. But at this position it will stop 32767 mins later. Right? Then what can i do? Self reset ? Or what? Or do i need another store? Unsigned long?

Why not use the Time library? It can easily derive HHMMSS times from millis().

Why are you counting like that? It is much easier and safer to calculate the values like this:

uint32_t seconds = millis() / 1000, minutes, hours;
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
1 Like

You might want to also look at variable types to understand the differences and range of values they can hold.
Google can help you here .

hammy:
You might want to also look at variable types to understand the differences and range of values they can hold.
Google can help you here .

Or just look at the Arduino documentation:

On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).(16 bits, -32k to +32k)

On the Uno and other ATMEGA based boards, unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 ((2^16) - 1).

Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.

Unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won’t store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1).

hannah_mackinlay:
... and guess how many seconds there are in 09:06:07?

thank you for help. 32767 seconds later it stop. Ok. I understood. I deleted seconds counter codes. But at this position it will stop 32767 mins later. Right? Then what can i do? Self reset ? Or what? Or do i need another store? Unsigned long?

aarg:
Why not use the Time library? It can easily derive HHMMSS times from millis().

How? I am new. Can you give me a sample?

Try changing these

int starttime;
int activetime;
int prevoustime = 0;

to 'unsigned long', instead of 'int'.

yusiskan:
How? I am new. Can you give me a sample?

Go to the Arduino Documentation. Look under "Libraries" and look for the DateTime library. All the information is there.