Pulling TimeStamp from the Serial Monitor?

Greetings all,

I'll keep this short and simple: Is there a way to grab the timestamp from the monitor in the IDE and save it out as a variable (e.g. for use in a file header)?

I'm using an Arduino UNO + Ethernet Shield v2 (for writing to SD card) on IDE v1.8.7 to record analog signals from a couple of sensors that are monitoring some environmental conditions (temp, humidity, pressure, etc.) during some testing.

I don't currently have an RTC module, but that is my backup plan. Due to some in-place regulations, I cannot grab time from an NTP server, and I will have to jump through a number of difficult hoops to get serial communications working.

Thank you for your time!

Do you want to type the time into the Serial monitor then use it in the program or something else ?

Not specifically looking to define a time in that manner.

When I start the Arduino, it goes through a few initialization steps and things - checking to see if things are connected, if the SD card is in place, if a certain file is present or not, etc. At each step, the serial monitor shows a timestamp that seems to come directly from the PC's clock. I'm looking to save those timestamps out without manual intervention.

Please post the code as it is now

Where do you want to save the timestamps to? The Arduino or the computer?

The code, though I’m not quite sure why it’s required for the general question:

#include <Time.h>
#include <TimeLib.h>
#include <SD.h>
#include <SPI.h>
#include "SdFat.h"

float tempF; 
float rH;
const int chipSelect  = 4;
unsigned long t; 

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized.");

  Serial.print("Deleting old file...");
  if (!SD.remove("Vaisala.txt")) {
    Serial.println("Vaisala.txt not found. Continuing collection.");
  }
  Serial.println("File deleted.");
}

void loop() {
  rH = analogRead(A0);
  rH = (rH / (1024/100))-0.8; //Range of rH is 0 to 100%.
  tempF = analogRead(A1);
  tempF = (tempF / (1024/140))-3; //Range of temp is 0 to 140F. 
  t = millis();
  t = t/1000;
  
  File dataFile = SD.open("Vaisala.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print(t);
    dataFile.print(",");
    dataFile.print(rH);
    dataFile.print(",");
    dataFile.println(tempF);
    dataFile.close();
    Serial.print(t);
    Serial.print(",");
    Serial.print(rH);
    Serial.print(",");
    Serial.println(tempF);
    delay(1000);
    }
}

The output in the serial monitor looks like:

08:41:53.603 -> Initializing SD card...Card initialized.
08:41:53.650 -> Deleting old file...File deleted.
08:41:53.697 -> 0,40.20,60.86
1,40.70,58.86
2,40.40,57.86
3,40.00,57.29
4,39.80,56.86

Ideally, I’d like to replace the first column of my output / text file with the timestamps. I figure that if I can save the first timestamp where values are collected from the example above (08:41:53.697), then I can manipulate that to show a timestamp for all subsequent lines (within a second, don’t need more resolution than that).

PDiddy:
The code, though I'm not quite sure why it's required for the general question:

Because, it's unclear (at least to me) where those timestamps are coming from or why they stop appearing.

Ahhh, a quick Google search found this: What triggers the timestamp in IDE 1.8.6 monitor? - Installation & Troubleshooting - Arduino Forum

I saw the same thread. My takeaway from that and some of the subsequent referenced pages was that timestamps were a buggy feature that now seem to be fixed to display in the serial monitor more predictably (correctly).

However, I still don't know if or understand how I can take that timestamp from the serial monitor and turn it into something useful.

Seems unlikely.

There is no direct way to grab the timestamps. Only your computer knows about them. This is not something generated by the Arduino board.

You could have a program running on your computer that sends the time to the Arduino via serial or Ethernet. Or you could just use the RTC.

Hello All,

If I understand you want to display timestamps on the serial monitor for each Serial.print.

Where would these stamps come from?
What would generate them?
Which would trigger these timestamps?

Regards,
bidouilleelec

bidouilleelec:
If I understand you want to display timestamps on the serial monitor for each Serial.print.

You don't understand correctly. They want to use their Arduino to write a timestamp to a file on an SD card. They noticed the Serial Monitor has a timestamp option and thought that information could be accessed from the Arduino board.

If you haven't noticed, In recent versions of the Arduino IDE (1.8.6 and onward I believe), there is a new "Show timestamp" checkbox at the bottom left corner. When you check this, each newline on the Serial Monitor is preceded by a timestamp. A very useful feature! The timestamp is generated from the computer's clock. In 1.8.7 (I can't remember whether 1.8.6 also), there was a bug that made the timestamp sporadic but this has been fixed in Arduino IDE 1.8.8.

Hello Pert

pert:
You don't understand correctly. They noticed the Serial Monitor has a timestamp option and thought that information could be accessed from the Arduino board.

If you haven't noticed, In recent versions of the Arduino IDE (1.8.6 and onward I believe), there is a new "Show timestamp" checkbox at the bottom left corner.

OK .
Thanks for your instantaneous answer.

For the moment I don't want to work with 1.8.6 or latter as I notice these versions were bugged.

Best regards,
bidouilleelec

1.8.8 is pretty nice.

There is a problem with the Arduino AVR Boards toolchain on Windows but you can easily roll that back to the last good version or else update to the new beta test version if you're feeling adventurous (instructions for both options here) so that's not really an IDE version-specific problem.

There is a minor issue that causes the previous sketch not to be opened when you start the IDE but I don't mind that.

There is a a problem with arduino-builder that causes automatically generated function prototypes to be inserted in the wrong place in certain rare circumstances but that was reported to go back to 1.8.6 so you'd already be affected by that bug.

Those are the only new bugs that come to my mind.

pert:
1.8.8 is pretty nice.
if you're feeling adventurous

I'm NOT adventurous.

Regards,
bidouillelec

Kind of in the same boat as PDiddy.

Got an ethernet shield and would like to timestamp SDcard file data logging.

Thanks for the info Pert on the Serial Monitor timestamp getting that from the PC.

Still do not have any idea though on how to put that in a usable Arduino variable ?

Found lots of old or half working NTP and manual examples that eat up 40%+ of the UNO 32K just to get a timestamp.
Was hoping for an easy solution since it is already working great in the Serial Monitor ????