Hot answers tagged

163 votes
Accepted

How can I handle the millis() rollover?

Short answer: do not try to “handle” the millis rollover, write rollover-safe code instead. Your example code from the tutorial is fine. If you try to detect the rollover in order to implement ...
user avatar
  • 36.9k
31 votes

How can I handle the millis() rollover?

TL;DR Short version: An unsigned long is 0 to 4,294,967,295 (2^32 - 1). So lets say previousMillis is 4,294,967,290 (5 ms before rollover), and currentMillis is 10 (10ms after rollover). Then ...
user avatar
  • 11.1k
17 votes

Why is my real time clock getting the wrong time from my PC?

The __DATE__ and __TIME__ are set when the code is compiling so they will naturally be behind since the code still needs to finish compiling and then be flashed to the chip. See the Arduino ...
user avatar
  • 7,283
16 votes

How do I get an accurate time?

You do not need an RTC to build a clock: the ATmega chip has all the hardware needed to perform the duties of the RTC itself. Here is how: Get a 32768 Hz watch crystal: either buy it or ...
user avatar
  • 36.9k
12 votes
Accepted

The difference between "time_t" and "DateTime"

A DateTime is a full class with lots of methods to it - a time_t is just an unsigned long. time_t is used to store the number of seconds since the epoch (normally 01/01/1970) The Arduino Time ...
user avatar
  • 103k
9 votes
Accepted

How do I run a loop for a specific amount of time?

Here is an example that will run for 5 minutes. Note that the loop will begin executing anytime before the time limit is up, including 1 msec before; it can't cut-off something happening at the 5-...
user avatar
  • 14.5k
9 votes
Accepted

The exact time for functions or CPU cycles for any function

Method summary (section added on 2018-01-28) There are several methods available for timing code. I am adding this preliminary section to my answer in order to provide comparative data on several ...
user avatar
  • 36.9k
9 votes
Accepted

Is there any better choice other than using delay() for a 6-hours delay?

Several options here, and a couple folks have pointed out some challenges. "Best" answer -- probably to use a real-time clock (RTC) board to assist your timing, and effectively set a target ...
user avatar
8 votes
Accepted

How much time does the Arduino need to call a Function or to read a Variable?

It is hard to give a general answer, as it depends on the specifics of your code and on how the compiler compiles it: How much time does an Arduino Uno or Mega need to only call a function. This ...
user avatar
  • 36.9k
8 votes
Accepted

Can I make delayMicroseconds more accurate?

As explained in the previous answers, your actual problem is not the accuracy of delayMicroseconds(), but rather the resolution of micros(). However, to answer your actual question, there is a more ...
user avatar
  • 36.9k
7 votes

Convert to and from Unix Timestamp

I think that the two most used libraries are the Adafruit RTClib and the pjrc.com TimeLib. They both have functions to convert the epoch, and they both are reliable. But both lack the timezone and the ...
user avatar
  • 3,087
6 votes
Accepted

Reading atomic radio clock

So, the first consideration goes to the radio propagation aspect. Every country does things a little different with their radio clocks. And you might only have good signal a few times a day. For a ...
user avatar
6 votes

Clock drift - is the precision of the crystal the only culprit?

The problem is the interaction between the interrupt handler that updates the value for the millis() function and the neopixels show() function. To maintain the counter for the millis() function a ...
user avatar
  • 2,090
6 votes

Resetting millis() and micros()

millis() and micros() overflow periodically. However, this is not a problem: as long as you compare durations instead of timestamps you can forget about the overflows. The liked answer also gives the ...
user avatar
  • 36.9k
6 votes
Accepted

Increasing number of iterations not increasing time

The compiler has realized that your loop doesn't do anything useful and therefore optimized the whole thing away. Thus you are just timing how long it takes to do nothing. (You didn't get a result of ...
user avatar
  • 35.3k
5 votes

How can I handle the millis() rollover?

I loved this question, and the great answers it generated. First a quick comment on a previous answer (I know, I know, but I don't have the rep to comment yet. :-). Edgar Bonet's answer was amazing. ...
user avatar
5 votes

Arduino Time Clock Accuracy

Re-visiting an old question... as I found a very informative blog post that sheds new light into it. But let me first provide some context before giving the link. When assessing the quality of a time ...
user avatar
  • 36.9k
5 votes

Resetting millis() and micros()

Overflow is never really an issue if you always calculate time difference. (Unless the time difference is more that 50 days.) unsigned long previousTime = millis(); ... wait for some event to happen ....
user avatar
  • 11.1k
5 votes
Accepted

Is there any way I can stop the Arduino Mega 2560 from resetting all variables after losing and reconnecting power?

Save the variables you need to EEPROM.
user avatar
  • 11.1k
5 votes

How to get current time and date in arduino without external source?

An accurate enough way is to use the millis() function. It will return the value in milliseconds since the start of the Arduino. If you start the Arduino at a specific time, you will be able to ...
user avatar
  • 2,803
5 votes

How to get elapsed seconds since Jan 1, 1970?

An arduino does not have a real time clock (RTC) built in. Every time it restarts it will reset the millis counter. You will need to add a RTC peripheral that you can poll to find the current date ...
user avatar
5 votes
Accepted

NTP rollover-safe design with ESP8266 (Curiosity)

This implementation is perfectly fine. The calculations are done with unsigned numbers, so they are naturally rollover safe. The day NTP time rolls over, secsSince1900 will become a very small number, ...
user avatar
  • 36.9k
4 votes
Accepted

Delay function without using a timer

delayMicroseconds(1000);// maximum delay is 65535 uS or #include <util/delay.h> _delay_ms(1);
user avatar
  • 11.1k
4 votes
Accepted

Should millis() be stored in a variable once each loop, or called for every reference?

The primary downside to calling a function each time is that it may take more CPU cycles to complete the overall loop. The difference in CPU cycles, if any, depends on the actual implementation of the ...
user avatar
  • 375
4 votes

My RTC is returning a sporadic date/time

The RTC library does not check for communications success. In particular, here: DateTime RTC_DS1307::now() { Wire.beginTransmission(DS1307_ADDRESS); Wire.send((byte) 0); Wire.endTransmission(); ...
user avatar
  • 35.3k
4 votes
Accepted

Can't store time over 9 hours and 10 minutes

Here's a hint: 32767 seconds is 9 hours, 6 minutes and 7 seconds. What is the largest value that an int can hold? EDIT: I can't add comments on this computer (old browser), so I have to respond to ...
user avatar
4 votes
Accepted

Why can't I declare variables inside an if statement?

Declaring a variable inside a block means that they only exist within that block. Once the block is exited, they become inaccessible.
user avatar
4 votes

How much time does the Arduino need to call a Function or to read a Variable?

How much time does an Arduino Uno or Mega need to only call a function. Why do you want to know? This sounds like an X-Y problem. It will vary, depending on a lot of things. The number of ...
user avatar
  • 35.3k
4 votes

How to get current time and date in arduino without external source?

without external source You can't. The Arduino Uno has no real-time clock. A real-time clock is only something like $1 from eBay. For example the DS1307 or DS3231. I found 5 x boards pre-assembled ...
user avatar
  • 35.3k

Only top scored, non community-wiki answers of a minimum length are eligible