Skip to main content
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 ...
Edgar Bonet's user avatar
  • 43.3k
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 ...
Shaun Vince's 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 ...
Edgar Bonet's user avatar
  • 43.3k
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 ...
Edgar Bonet's user avatar
  • 43.3k
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 ...
Jot's user avatar
  • 3,246
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 ...
Edgar Bonet's user avatar
  • 43.3k
6 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. ...
brainbarker's user avatar
6 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 ...
Dat Ha's user avatar
  • 2,913
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 ...
Nick Gammon's user avatar
  • 38.4k
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.
Gerben's user avatar
  • 11.3k
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 ....
Gerben's user avatar
  • 11.3k
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 ...
Edgar Bonet's user avatar
  • 43.3k
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 ...
ratchet freak's user avatar
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 ...
Dave Tweed's 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.
Ignacio Vazquez-Abrams's 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 ...
Nick Gammon's user avatar
  • 38.4k
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 ...
Nick Gammon's user avatar
  • 38.4k
4 votes

Can I make delayMicroseconds more accurate?

Your test results are misleading. delayMicroseconds() actually delays fairly precisely (for delays of more than 2 or 3 microseconds). You can examine its source code in file /usr/share/arduino/...
James Waldby - jwpat7's user avatar
4 votes
Accepted

How to parse 20180810T143000Z to time_t

Parsing the string really is the only way. However there are many ways of doing it. My preferred method is to first check that the format is correct, by looking for the T and Z being in the right ...
Majenko's user avatar
  • 105k
4 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, ...
Edgar Bonet's user avatar
  • 43.3k
4 votes

Set time() on ESP8266

Whenever you have epoch time, either from RTC, NTP, etc., use the POSIX function settimeofday() to set system time as @Majenko said The sample code is as follows: // Assuming we already have Unix `...
khoih-prog's user avatar
4 votes
Accepted

How to efficiently code a long duration timer

Should I still be using millis() or is there a more efficient way to create a timer which tracks these longer time periods? For longer time periods you really should use an RTC. You will get much ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Attiny timer interrupt does not make an interrupt every 1000ms

I see three issues with this approach. The first is that you are using a very low quality, uncalibrated time source. The frequency of the internal RC oscillator is good to within a few percent only. ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

How to keep accurate millis() while using ADC_sleep mode?

Rather than trying to update TCNT0, it might be better to track the number of cycles lost to ADC conversions, and in an intermediate routine – eg, omillis() – compensate for those cycles. [Edit: See ...
James Waldby - jwpat7's user avatar
3 votes
Accepted

How to improve date-time control from RTC?

Is there a simple way to convert to date-time object? Not with this library. But you can find other libraries supporting the DS3232 which work with the Arduino Time library[]. This time library ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

Format hour and minute integers to HH:MM format (char*)

You don't want a char *, you want a character array, i.e. you have to allocate the memory for the string, not only for a pointer. That being said, you can build the string one character at a time like ...
Edgar Bonet's user avatar
  • 43.3k
3 votes

Arduino creating delay function

You can try something along these lines: void verboseDelay(unsigned long ms) { unsigned long now = millis(); Serial.print(F("delaying")); unsigned long start = now, lastPrintTime = now; ...
Edgar Bonet's user avatar
  • 43.3k
3 votes

X and Y axis on Arduino Serial Plotter?

Inorder to plot multiple variables or waveforms simultaneously a 'space' is printed between the two print statements. Serial.print(counter); Serial.print(" "); time = millis(); Serial.println(time); ...
Hasan's user avatar
  • 1,486
3 votes

Can I make delayMicroseconds more accurate?

I'm checking to see how good the Arduino is at delaying... Seems to be pretty terrible at it. micros() has a well-documented resolution of 4 µs. You could improve the resolution by changing the ...
Nick Gammon's user avatar
  • 38.4k

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