Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use time library in Arduino?

Tags:

c++

c

arduino

I am trying to upload this simple program to my Arduino that gets to clock time:

#include <Time.h>

time_t nowTime;

void setup() {
    nowTime = now();
}

However, its failing to compile:

exit status 1
'now' was not declared in this scope

Why is now() not declared in this scope? The Time.h file was included. So why wouldn't now() be declared? How can I get around this?

like image 354
Saqib Ali Avatar asked Sep 06 '25 23:09

Saqib Ali


1 Answers

The compilation fails because the Time.h file that the compiler finds has nothing to do with time libraries such as that by Paul Stoffregen (https://github.com/PaulStoffregen/Time).

I tried your Sketch, compiled for an Arduino Uno, and saw the same error you see: that Time.h resolves (the file exists somewhere), yet now() is not defined by that Time.h

After searching my Windows PC for a while, I finally found what I think is the file that #include includes on my installation: C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\firmwares\wifishield\wifiHD\src\time.h or perhaps C:\Users\Brad\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\time.h

Neither of those files defines the now() function.

If you want to use Paul Stoffregen's Time library, download and install it from https://github.com/PaulStoffregen/Time. If instead you wish to use Michael Margolis' Time library, you can find and install it in the Arduino IDE, under Tools / Manage Libraries... and entering "Time" (without quotes) in the search term.

As others have pointed out, the Arduino environment doesn't always know the current date and time. The functions mills() and micros(), return the number of milliseconds or microseconds, respectively, since the Arduino booted. For just looking at the passage of time, most people use millis() or micros() instead of a more complex library.

like image 58
Bradford Needham Avatar answered Sep 09 '25 03:09

Bradford Needham