Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [millis]

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

Filter by
Sorted by
Tagged with
132 votes
4 answers
108k views

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 ...
Edgar Bonet's user avatar
  • 43.2k
20 votes
5 answers
10k views

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 ...
John Walthour's user avatar
15 votes
3 answers
5k views

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 ...
asheeshr's user avatar
  • 3,837
14 votes
4 answers
53k views

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 ...
Petr's user avatar
  • 253
14 votes
1 answer
3k views

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 ...
Programmer's user avatar
10 votes
3 answers
12k views

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 ...
user3060854's user avatar
9 votes
2 answers
4k views

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's user avatar
  • 295
7 votes
4 answers
2k views

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(...
Splitframe's user avatar
6 votes
3 answers
5k views

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 ...
Kozuch's user avatar
  • 167
5 votes
4 answers
7k views

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 ...
Anonymous Penguin's user avatar
5 votes
1 answer
820 views

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 ...
Cyril_Ram's user avatar
5 votes
4 answers
3k views

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. ...
FarO's user avatar
  • 339
5 votes
4 answers
22k views

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 ...
HiWay's user avatar
  • 109
4 votes
4 answers
11k views

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 ...
noearchimede's user avatar
3 votes
3 answers
33k views

Resetting millis() and micros()

I want to know how much time has elapsed since a certain event, and I do not want to use any external timers. It seems logical to reset an internal timer whenever the event occurs (using an interrupt)....
pseudoeuclidean's user avatar
3 votes
2 answers
283 views

How to use Multipile millis()

I've a exhaust fan that runs when the humidity is high. When the exhaust fan is on it should work for 3 minutes and should wait another 5 minutes to on. I am a beginner to this struggling for almost 2 ...
user2037091's user avatar
3 votes
1 answer
786 views

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 ...
Myles's user avatar
  • 73
3 votes
5 answers
8k views

Using millis(), in nested loops

I am using millis() to flash some lights (neopixels) on an arduino pro mini. The problem is that I cannot flash the lights for the desired period. More specifically, the lights flash for ~250 ms, ...
Tahseen's user avatar
  • 51
3 votes
4 answers
14k views

Measuring Time Between Inputs

I'm trying to measure the amount of time that has passed between every time a reed switch mounted on a wheel is activated to calculate the speed of a bike in miles per hour and display it on two 7 ...
CodeRead's user avatar
  • 177
3 votes
2 answers
1k views

timer0_millis always returns 0

I am trying to make a simple debounce function as explained here but the condition if((millis()-timeCheck)>reboundDelay) is never met. As you can see in my code below, I am trying to control a PWM ...
Alexandre Rivara's user avatar
3 votes
1 answer
9k views

How can I replace pulseIn with interrupts?

I have an HC-SR04 sensor and I need to get the distance from it. I use this code: digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, ...
shurup's user avatar
  • 300
3 votes
1 answer
91 views

Fader OOP Problem

This code works and it can fade three LEDs without any problem, obviously you can even add more of them : ) // Pins Definition const int button1 = 13; const int button2 = 12; const int button3 = 11; ...
alessandromrc's user avatar
3 votes
0 answers
110 views

Why would using the += operator on an unsigned long crash my sketch? [closed]

I wrote a simple Timer class that's designed to work even in the face of the 49 day wrap of the millis() counter. I wanted a library that I could easily re-use and I could never remember the right way ...
Tim Long's user avatar
  • 305
2 votes
2 answers
1k views

How to write nonblocking code, for polling sensor at 100 Hz

I'm using this piece of code to try to poll an IMU sensor at 100 Hz (for a AHRS sensor fusion library). void loop(void) { // nonblocking code variables static uint32_t last_ms; uint32_t ms; // ...
Zhelyazko Grudov's user avatar
2 votes
1 answer
5k views

How to set the sampling frequency in Arduino without using a delay

I am reading an ECG signal using a single lead heart rate monitor. What I know is that when I use delay(15) it means that I am getting 66.67 readings in a second which corresponds to the sampling ...
Ahmed K. Moustafa's user avatar
2 votes
1 answer
538 views

How to Independently run positional and continuous servos using millis()?

I am setting up 3 servos (2 position servos and 1 '360' continuous rotational servo) with Arduino. but I failed to run these independently. It runs one after another. I wish to run it in loop ...
Emlinux's user avatar
  • 43
2 votes
1 answer
1k views

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

When comparing the value of millis() numerous times per loop(), are there reasons for/against storing it in a variable once at the top of loop(), v.s. calling millis() each time? The most basic ...
Bort's user avatar
  • 397
2 votes
1 answer
6k views

nodeMCU - Millis() - Simple Counter - How long is LED on?

PROGRESS, now I have this (WITH GREAT HELP): void setup() { Serial.begin(9600); // initialize digital pin 13 as an output. } uint32_t timer_start = 0; int timer_running = false; void loop() { // ...
AgoraLive's user avatar
2 votes
1 answer
3k views

Does millis() work inside a function?

So I know that millis() doesn't work how it's suppose to if you use it in an interrupt but lets say we have a code that looks like below, will millis() work properly or how it behaves in an interrupt? ...
Brit 's user avatar
  • 41
2 votes
1 answer
11k views

Button with switch timer and reset time function + LED feedback. What am I doing wrong?

Beginner here. I´ve been trying to implement a button function timer working with millis() instead of delay as I want the button to be responsive while the timer is running. So far I´ve managed to ...
ifthisthenthat's user avatar
2 votes
2 answers
81 views

Trying to keep my main code running unless an input is LOW for a certain amount of time

I have code that creates a sequential signal with 4 LEDS. It cascades to the right. This works when I hold down the input button. What I need help with is having the code run exactly the same when ...
Myles's user avatar
  • 73
2 votes
2 answers
842 views

Millis() does not back to zero after woke up from deep sleep mode

I am going to make a project that needs four boards of Arduino which synchronization in time is needed. The project is about capturing the timestamp (in ms resolution) whenever something is crossing ...
jendiiw's user avatar
  • 21
2 votes
1 answer
928 views

Pause in the program for some time

I have a variable timeInSec which records the time as loop begins. It is defined as timeInSec = millis();. I have two conditions. If (timeInSec - lastRun)< 5 (that is 5 sec) , the first condition ...
Sri Harsha's user avatar
2 votes
2 answers
1k views

Measurement of pulse duration greater than 3mins

Is it possible to measure the width of a pulse which is longer than 3 minutes? The pulseIn function can only measure duration of maximum 3 minutes accurately. I've tried using interrupts but haven't ...
Navin Sridhar's user avatar
2 votes
2 answers
816 views

Toggle button switches through case statements with button debouncing

The overview of my code is that I want a toggle button to be pushed and each button push will move the code to the next case statement. In each case statement, there will be different LED functions. I ...
Myles's user avatar
  • 73
2 votes
1 answer
332 views

Is there something that I need to do to make the code from this answer compile?

I tried to compile the following code (using Arduino 1.8.16 with an esp8266-12) extern volatile unsigned long timer0_millis; unsigned long new_value = 0; void setup(){ //Setup stuff } void loop(){ ...
TRS-80's user avatar
  • 21
2 votes
1 answer
101 views

Relays just switch ON and OFF on delay(), not on millis()

I use fuzzy logic for the running time of relays, but the relays just switch ON and OFF at the interval of the delay() I put at the end of loop. This is my code: void loop () { ph = -180/100.0; ...
daffa faiz's user avatar
2 votes
1 answer
241 views

Help with millis to get accurate timing

I'm essentially trying to create a music box which is light activated using an LDR. When it's open a song plays, while a servo rotates a figurine back and forth, and an LED dims and illuminates in ...
TonyStark's user avatar
2 votes
3 answers
461 views

How to use millis() in my code instead of delay()?

I'm having issues trying to convert my code to non-blocking. I was able to do so in some parts, but the rest aren't the same. When I try to replace delay with an if statement like the spinning and ...
KD Technology's user avatar
2 votes
1 answer
303 views

Can't use millis() instead of delay

I am making a home coming/leaving light for car. First I want that whenever I press the car key lock, a timer will start for 10 seconds. And if I press a button twice within 10 seconds, pin 13 will ...
aadesh dahiya's user avatar
2 votes
3 answers
399 views

How can I find the time taken while a function is being called? [duplicate]

I tried using millis, but I wanted to know if there is any better way to find out the exact time from when a function is called and finally when it terminates.
Diyanko Bhowmik's user avatar
2 votes
1 answer
127 views

millis() vs creating a "long" variable

I think my issue is not fully understanding how the millis() function works; I am wondering the difference between simply creating a variable such as long fake_millis ++ ; vs using millis()?
danieljay's user avatar
2 votes
1 answer
458 views

How to use millis()

I'm trying to have a keypad do two one of three things when a key is pressed: If the 1 is pressed I want LED 1 to blink twice then stay on If either the 2 or the 3 is pressed I want LED 2 to blink ...
finlay's user avatar
  • 23
2 votes
1 answer
187 views

How to start a block of code after a certain time?

I'm using a ATMega328P-PU on a breadboard as an arduino. How do you start a code after say 20 seconds have elapsed since startup? Example code:- if(condition) { //Start sending morse code! } My ...
Parth Sane's user avatar
2 votes
2 answers
1k views

Arduino time(milli) and pressure sensor

I've succeeded to get an Arduino to work with a pressure sensor. What I want to do is when I put pressure on the sensor, after 5 minutes the led lights on. it starts over again if there's no ...
peruwan's user avatar
  • 21
2 votes
2 answers
1k views

smooth DC motor control with millis

So the task is to control 12v DC motor. With first button, you can add up motor speed by certain speed step. With second button, you can slow down motors speed by certain speed step. With third button ...
Jānis's user avatar
  • 59
2 votes
0 answers
520 views

Millis() is Not Working

I have a sketch that uses two timers using millis() from a library I created to keep time. The timers seem to be working correctly, but when I print the start time of each timer, it is clearly wrong. ...
Tarlyo's user avatar
  • 21
2 votes
0 answers
100 views

Storing random and different values of millis() while inside a loop? [closed]

Is it possible to store a value of time while you're inside a loop? Entertain the condition: if (condition && value > 0) { timeTrans = millis() + 2000; timeTrans - millis() = value; } ...
Tarik Agcayazi's user avatar
2 votes
1 answer
121 views

PIR sensor code logic works on Mega but not on Yún

I am trying to use this example(it works on Arduino Mega): millis() - lowIn comes greater than 5000when motion ends. ///////////////////////////// //VARS //the time we give the sensor to calibrate (...
Ciasto piekarz's user avatar
2 votes
0 answers
116 views

Start LED blink at certain millis() value

I was looking at the blink without delay sketch and I was trying to modify it so the LED would start blinking after 1000 ms. I changed the previousMillis variable to 1000, but the program didn't ...
tootin' putin 's user avatar