Questions tagged [millis]

Is a standard library function. It returns the number of milliseconds since the Arduino board began running the current program.

Millis is a standard library function. It returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

For more, see the standard reference.

179 questions
135
votes
4 answers

How can I handle the millis() rollover?

I need to read a sensor every five minutes, but since my sketch also has other tasks to do, I cannot just delay() between the readings. There is the Blink without delay tutorial suggesting I code along these lines: void loop() { unsigned long…
Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
20
votes
5 answers

How do I get an accurate time?

I've made a clock using an Arduino, but the time seems to drift. I am aware of the rollover issue; the clock seems to drift by about 15 minutes over the course of a week. I'm using a custom PCB with this resonator from Digi-key. The code reads the…
John Walthour
  • 323
  • 1
  • 3
  • 8
15
votes
3 answers

Is it possible to find the time taken by millis?

The function millis would be running in the span of 100+ microseconds or less. Is there a reliable way to go about measuring the time taken by a single millis call? One approach that comes to mind is using micros, however, a call to micros will…
asheeshr
  • 3,847
  • 3
  • 26
  • 61
14
votes
1 answer

How millis() resets itself to 0

Looking at the documentation for the millis() function , it says: Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. How's…
Programmer
  • 362
  • 4
  • 11
14
votes
4 answers

Using millis() and micros() inside an interrupt routine

The documentation for attachInterrupt() says: ... millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially, but…
Petr
  • 253
  • 1
  • 2
  • 12
10
votes
3 answers

arduino - millis()

Copied from the Arduino reference - millis() Tip: Note that the parameter for millis is an unsigned long, errors may be generated if a programmer tries to do math with other datatypes such as ints. What kind of math? What kind of other type of…
user3060854
  • 600
  • 4
  • 9
  • 20
9
votes
2 answers

Does millis() conflict with the PWM pins associated with timer 0?

I've read that the millis() function uses the same timer as a couple of PWM pins. If you're using those PWM pins, will millis() still return the correct value?
Aurast
  • 295
  • 2
  • 7
7
votes
4 answers

Adjust time calculation after Timer0 frequency change

I have an Arduino Nano with an 328P and need all 6 PWM pins. Thus, I had to adjust the prescaler and WGM Mode of Timer0. It is now in phase correct PWM mode with a prescaler of 1. TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00); TCCR0B =…
Splitframe
  • 173
  • 5
6
votes
3 answers

Visual clock with millisecond display refresh rate

I need to create a millisecond capable visual clock - I will use the clock for visual frame synchronization of multiple cameras. I found this project but it seems to use a LED display that only has resolution of one 1/100th of a second. Not sure if…
Kozuch
  • 167
  • 1
  • 6
5
votes
4 answers

millis() function with a button press

I am trying to use the millis() function to turn on a pin for a specified interval then turn off and turn on a second pin. It is intended to power a relay and offer a visual cue to when the cycle is over. Several of these need to eventually be…
HiWay
  • 109
  • 1
  • 1
  • 11
5
votes
1 answer

Delay() and millis() functions don't work in cpp external files

I'm currently facing an issue using delay() and millis() functions in an external cpp file. The issue is that when I used delay() in my main program (meaning .ino file) it works well but when I call the same function in an external cpp file, I'm…
Cyril_Ram
  • 53
  • 2
5
votes
4 answers

Is `millis()` affected by long ISRs?

I have a project that uses timers and interrupts frequently. A lot of CPU time is spent handling ISRs over an extended period of time. Would this affect code inside the main loop that relies on the millis() function since a significant amount of…
Anonymous Penguin
  • 6,365
  • 10
  • 34
  • 62
5
votes
4 answers

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

millis() uses timer0 (linked to CPU clock) to count time, but ADC_sleep mode stops the CPU clock, therefore millis() will drift (lag behind) after each ADC conversion performed in ADC_sleep mode. With the standard number of CPU cycles needed for the…
FarO
  • 339
  • 1
  • 4
  • 16
4
votes
4 answers

Use timer0 without affecting millis() and micros()

I'm writing a library which needs an ISR to turn off an LED some time after it was turned on. Since it's all about turning an LED on and off it doesn't need to be very precise. On the other hand I would like to use this library in a program where…
noearchimede
  • 482
  • 2
  • 7
  • 19
3
votes
1 answer

How do I run 4 LEDs sequentially based off of a push button input?

I am trying to create a program that runs 4 LEDs sequentially while also being able to do other things with different inputs and outputs. Because of this, I am using the millis function and not the delay. I have the code working but not exactly how…
Myles
  • 73
  • 7
1
2 3
11 12