Time keeper

Hello,

For a project of mine i would like to keep track of the time, meaning only the hour and minute, i don’t need to know the exact date.

I have seen that for the exact date I would need to use a RTC but i would like to try without bying one.

Therefore I’m trying to use the millis fonction to take the time into account, i thought of the following code.

void day_time{  
  actual_millis=millis();
  if (actual_millis>previous_millis){
    if (actual_millis-previous_millis+hour<24000){
      hour+=(actual_millis-previous_millis);
      previous_millis=actual_millis;
    }
    else {
      hout=(actual_millis-previous_millis);
      previous_millis=actual_millis;
    }
  }
  else{
    if (actual_millis+hour+4294967295-previous_millis<2400){
      horloge+=(millis_actuel+4294967295-millis_precedant);
      previous_millis=actual_millis;
    }
    else{
      horloge=actual_millis+4294967295-previous_millis);
      previous_millis=actual_millis;
    }
  }
}

For now i took for test and quickness 1 second <-> 1 hour

If anyone has an idea how to make it work or is it for sure not feasable without an RTC

Just get the Time library!

It is feasible without an RTC, but using any board that uses an internal oscillator instead of a crystal, such as the UNO, will result in some drift of the time. A DS3231 RTC does not need a crystal, and will give you 1 minute per year accuracy. For short runs however, counting millis will work.

This example prints the elapsed hours, minutes and seconds in the serial monitor

unsigned long previousMinuteMillis = millis();
const long aMinute = 60003L;  // calibrated from 60000 for MY arduino.

byte minutes = 0;
byte hours = 0;

void setup() {
  Serial.begin(9600);
  delay(500);
  Serial.println(F("Elapsed Time: HH:MM"));
  Serial.println();
  Serial.println(F(" 00:00"));
}

// print time elapsed since startup
void loop() {

  // Do this every minute
  if (millis() - previousMinuteMillis >= aMinute) {
    previousMinuteMillis += aMinute;
    if (minutes++ > 59) {
      minutes = 0;
      if (hours++ > 23) hours = 0;
    }
    Serial.print(' ');
    serialPad(hours);
    Serial.print(':');
    serialPad(minutes);
    Serial.println();
  }

}

// print leading zero if less than ten
void serialPad (byte n) {
  if (n < 10) Serial.print('0');
  Serial.print(n);
}

Of course you can roll your own like that, but if you want the bells and whistles, e.g. years, months, calendar days, day of the week, comparison of times and many other things, the Time library is a charm. Especially if you have no RTC.

aarg:
Of course you can roll your own like that, but if you want the bells and whistles, e.g. years, months, calendar days, day of the week, comparison of times and many other things, the Time library is a charm. Especially if you have no RTC.

It is, but OP specified hours and minutes only. I do use the Time library (with an RTC) in several clocks, and it makes quick work of standard timekeeping/calendar. For a minimal sketch though, it's pretty easy to just count and roll over.

OT: I live in Arizona, and we don't have daylight time here, so I never check. Does the Time library do DST?

ChrisTenone:
OT: I live in Arizona, and we don't have daylight time here, so I never check. Does the Time library do DST?

No, but in fairness RTC's don't either.

Thanks. Hopefully it will be abolished soon.

ChrisTenone:
Thanks. Hopefully it will be abolished soon.

Where I am now, it was abolished in 1992. :slight_smile:

Oh, good. I was done with it in the '60s. But every other state (and a lot of countries) still do it, so I'm never absolutely sure of what time it is elsewhere.

ChrisTenone:
Does the Time library do DST?

No. but you can use the Timezone library to add it.

I've used both DS3231 and DS1307 RTC modules and the latter (DS1307) drifts over the course of a month by up to a minute or more - depending on the temperature. That was quite annoying which is why I no longer use that module and stick with the DS2131 which is temperature compensated.

So if I found that using a slowly drifting RTC was mildly annoying I'm guessing you will be thoroughly fed up in about 2 weeks' time as the drift in the Arduino's timekeeping becomes apparent.

As an academic exercise it's great fun (and the time library is great) but an Arduino is no RTC (not even a DS1307) so if you really are going to want to use this to keep (accurate) time seriously consider the fit-and-forget DS3231!

In fact, I'm almost considering abandoning RTCs altogether in favour of web-server modules (ESP8266) that can get the ultra-accurate time (DST, Date and everything else thrown in for free) or a GPS module (ditto) for not that much more money than the RTC. Depends on how accurate you need the time to be, I guess.

Just suggestin' :slight_smile:

Ralph_S_Bacon:
In fact, I'm almost considering abandoning RTCs altogether in favour of web-server modules (ESP8266) that can get the ultra-accurate time (DST, Date and everything else thrown in for free) or a GPS module (ditto) for not that much more money than the RTC.

a ds3231 can easily be purchased for less than $1 USD (around 0.83) shipped to my door.
Can you actually get one of those other modules for anything close to that price range?

--- bill

bperrybap:
a ds3231 can easily be purchased for less than $1 USD (around 0.83) shipped to my door.
Can you actually get one of those other modules for anything close to that price range?

— bill

I wish!

But the ESPduino (or similar) I use would of course replace the Arduino in its entirety. Unfortunately the GPS module cannot be had for less than GBP6 (at today’s lets-vote-to-leave-the-EU rate) about $9, I guess. But it’s all in the same ball-park (I envisage a rather large ball park, it must be said).

That said, the convenience factor of having the totally correct date and time to hand forever more must be individually weighed for the application in hand. For me, either solution would be preferable to the twice-yearly DST adjustments - how great it is that most of my home appliances all set themselves these days (including alarm clocks) and the odd one or two that don’t are are right pain the derriere, not least because they always get forgotten until two weeks after the clocks change! Doh!

Another reason to avoid the DS1307 and use the DS3231 is i2c clock speed.
The DS1307 is REALLY old and only supports a 100khz clock. Most i2c slaves support 400khz.
So if you use the DS3231 instead, you can run the i2c bus at 400Khz instead of 100Khz.
This can be a benefit particularly if sharing the bus with other slaves like an i2c LCD or a NVRAM.

IMO, at this point the DS1307 should be avoided, particularly since the DS3231 is typically the same or lower cost in pre-built modules and also supports temperature readings.

--- bill

I prefer to use a DS3231.
When I want a timekeeping device that relies on signals from the sky, I'll use a sundial.

The DS3231 is a no brainer these days, but the very common ZS-042 requires modifications to work properly: ZS-042 DS3231 RTC module - Sensors - Arduino Forum

My clocks run hands free and accurately year round, with the DS3231 and the TimeZone library. I consider a clock to need setting if it's a few seconds off, so about once a year does it. Some of them have a radio receiver, so setting them is just a matter of turning on the GPS time transmitter for a few minutes. No fuss, no bother.

Edit - this is the link I was really looking for: Using a $1 DS3231 Real-time Clock Module with Arduino | Underwater Arduino Data Loggers

If you are fanatic about accuracy, and don't want to hook up to a radio or network or gps to set the time, ebay has a bunch of rubidium atomic oscillators for around $200.

Hi guys,

i have 3 level water sensors, low , mid, full. for each level, the user is able to control the pump to be on or off. there is only 1 pump implemented. i would like to track the 'on' time of the pump. if i edit this code to be a function and call it each level, will the clock runs contentiously from where it stop or it will run from 0 each time the function execute?

thanks

hi guys and ChrisTenone,

do you guys create a reset function the timer?
could you guys pls guide me with this.

thanks