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
109
votes
4answers
81k 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 ...
17
votes
5answers
9k 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 ...
14
votes
1answer
2k 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 ...
13
votes
3answers
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 ...
13
votes
4answers
43k 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 ...
10
votes
3answers
11k 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 ...
9
votes
2answers
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?
7
votes
4answers
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(...
6
votes
3answers
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 ...
5
votes
4answers
6k 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 ...
5
votes
1answer
394 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 ...
5
votes
4answers
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. ...
5
votes
4answers
20k 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 ...
3
votes
3answers
29k 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)....
3
votes
4answers
9k 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 ...
3
votes
2answers
253 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 ...
3
votes
4answers
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 ...
3
votes
2answers
923 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 ...
3
votes
1answer
7k 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, ...
3
votes
1answer
83 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; ...
3
votes
1answer
109 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 ...
3
votes
0answers
80 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 ...
2
votes
2answers
905 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; // ...
2
votes
1answer
4k 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 ...
2
votes
5answers
7k 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, ...
2
votes
1answer
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 ...
2
votes
1answer
5k 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() { // ...
2
votes
1answer
2k 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? ...
2
votes
1answer
9k 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 ...
2
votes
1answer
878 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 ...
2
votes
2answers
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 ...
2
votes
1answer
89 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(){ ...
2
votes
1answer
62 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; ...
2
votes
3answers
144 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 ...
2
votes
1answer
241 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 ...
2
votes
2answers
371 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 ...
2
votes
3answers
342 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.
2
votes
1answer
115 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()?
2
votes
1answer
424 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 ...
2
votes
1answer
162 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 ...
2
votes
2answers
314 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 ...
2
votes
0answers
354 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. ...
2
votes
0answers
93 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; } ...
2
votes
1answer
97 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 (...
2
votes
0answers
104 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 ...
1
vote
2answers
265 views

Switch on the piezo emitter for a short time

I need a short beep for every minute of change. I wrote a separate function. The signal duration should be 50 ms, the rest of the time the emitter connected to the A1 contact should be silent. A ...
1
vote
3answers
1k views

How to rewrite code in millis?

I am currently working on a project that makes a plateua move back and forth using a stepper motor. On this plateau I have attached LEDs that create patterns. However, the plateau should be moving ...
1
vote
4answers
1k views

need help for saving the millis function when arduino was rebooted

I have two questions want to ask I'm using Arduino Uno with 16*2 LCD Blue ( also I have temp. sensor and two motors also 3 limit switches) ,, my code is working good, but sometimes my lcd stop ...
1
vote
3answers
119 views

Time counting using millis

I am trying to write a code where photoresistors (a or b) sense blinking of the light, and lights LED (k and d) simultaneosly with that blinking. And I want that when both photoresistors sense dark ...
1
vote
2answers
3k views

How can I made my Arduino follow the time exactly?

I am making an Arduino clock. You can set time to it, and then it keeps on going. The problem is that it is always within a minute off. It is not a major issue, but one that I would like to fix. I use ...