Keeping relative time (simply) without RTC

Hello,

I am very new to Arduino, but still wanted to share my workaround.

I will be following sensor parameter values as a function of time in minutes since the start of my experiment (data acquisition). I don't care about the clock, I just need elapsed time in minutes with the computer ON and powering my Arduino UNO.

  1. I downloaded the following library
    GitHub - PaulStoffregen/Time: Time library for Arduino

  2. I wrote the following simple sketch
    sketch_oct10aTimeEasy.ino (541 Bytes)

  3. to reset at the start of your experiment, hit 'clear input' on serial monitor then hit 'upload'.

  4. Using Windows10, I can keep the experiment running while away from the computer - LOCK computer with the COM3 page open in the foreground. Sleep does not work as there is a gap in data acquisition.

I hope it helps!

Alex

sounds like simply need a timestamp for each measurement.

millis() provides the elapsed # of msec since the arduino started. send/print the value of millis() along with the measurement

That's fine, only rarely is a clock actually needed, but, in the absence of posted code it is hard to tell what you do care about. Guessing that your loop is a minute long, all you need do is include a ++ count and send that as data. Also, since it seems you are connected to PC, you can use a kosher terminal that not only records the data but also timestamps each package received, using the PC clock. Terminals, like RealTerm, Putty etc., all do this and all are freebies. You can also send the data direct to Excel, and stamp that with an Excel command. In these instances, the timestamp is the full bottle, but that doesn't cost any extra, and may even be useful.

The data type of now() is time_t, which is an unsigned long.

I usually use the RTClib library, because it has a millis-based software RTC that allows for writing code and debugging without having an actual hardware RTC, then later add the hardware RTC by changing the constructor and a line or two in setup.

This is how you post your code

#include <TimeLib.h>

int delay_sec=5;  // how many seconds between time samples?

void setup() {
 Serial.begin(9600);

}

void loop() {
  float Elapsed_sec=now(); //important to define these as 'float' for precision
  float Elapsed_min=Elapsed_sec/60; //important to define these as 'float' for precision
  Serial.print(Elapsed_min, 2); //time in min with 2 digits after comma
  Serial.print(" ; ");
  Serial.print(Elapsed_sec, 0); //time in min with 0 digits after comma
  Serial.println(); 
  delay(delay_sec*1000);

}

There are a few problems with this.
Firstly, its "blocking" code - so while in delay() it can do nothing else;
more importantly, if your program does anything else (eg the Serial.print(Elapsed_min, 2); )
(and Serial.begin(9600); is a very slow data rate)

or eg a data measurement the loop time will be 1000msec + other bits.

If you look at the BWOD sketch

you will see how you can use millis() to get the timing right - see how it uses the "interval" parameter.

Good explanation of using millis() here

Thank you all for your input. The function millis() is indeed elegant!