Skip to main content
25 votes
Accepted

How millis() resets itself to 0

It is not an exclusive property of millis(). Any counter with a limited number of digits eventually returns to zero. For example, a 4 digit tally counter returns to zeros after 9999. Under the hood, ...
Juraj's user avatar
  • 18k
10 votes
Accepted

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

The Arduino framework does some initializing for you before going into setup() and loop(), for example configuring Timer0 correctly for millis(), delay() and siblings. This is done in the main() ...
chrisl's user avatar
  • 16.3k
8 votes
Accepted

Use timer0 without affecting millis() and micros()

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions, Yes. A few ways, depending on your level of comfort: You can declare the stock Arduino Timer0 OVF ...
dannyf's user avatar
  • 2,770
7 votes
Accepted

Why doesn't millis() use 1 µsec or 1 msec interrupt?

One microsecond is only 16 CPU cycles. The CPU needs 4 cycles to prepare itself for servicing the interrupt (save the program counter, load the interrupt vector and clear the I bit in SREG). 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

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
Accepted

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

should I be declaring last_ms as a global variable instead of static? Local static is better, as it limits the scope of the variable to just where it is needed. Consider making it global only if the ...
Edgar Bonet's user avatar
  • 43.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
Accepted

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

Use the concept of blink without delay. Instead of using delay() to set the timeout you use a timestamp and check micros() against that to see whether your interval has passed: const unsigned long ...
ratchet freak's user avatar
5 votes
Accepted

How to use Multipile millis()

If I understand correctly, you wrote an example code that switches an LED between two states (on and off), and you are stuck because the actual problem you want to solve is more complex than that. And ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

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

To be honest, I don't understand exactly the problems, but I try to give some guideline and hope you can use the idea to fix your problems. What you need is a so called 'state machine'. I think (...
Michel Keijzers's user avatar
4 votes
Accepted

Issue in long ON OFF timer using millis()

You declare interval as an integer, while onTime and offTime are unsigned long. Change int interval = onTime; to unsigned long interval = onTime; The maximum value of int is only 32767 (215-1), ...
Gerben's user avatar
  • 11.3k
4 votes

Running Arduino with 2 outputs and 2 inputs

First thing: you should call pulseIn() right after sending the TRIG pulse. If you wait too much, you will miss the start of the echo pulse. For instance, here: int distance = pulseIn(ECHO, HIGH, 26000)...
Edgar Bonet's user avatar
  • 43.3k
4 votes

Switch on the piezo emitter for a short time

Your myTimer1 in t = (millis() - myTimer1) / 1000; is not set
Qareke's user avatar
  • 104
4 votes
Accepted

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

This is covered in the Blink Without Delay Arduino tutorial. Let me paraphrase the main part of the example code here: uint8_t ledState = LOW; uint32_t lastTimeLedChanged = 0; void loop() { ...
Edgar Bonet's user avatar
  • 43.3k
4 votes
Accepted

Assign a delay value generated by a random function called randomBlink()

Your problem descriptions is a bit unclear, though I think I see your problem. On every loop() iteration you call randomBlink(), which generates a new random delay value and then calls ledBlink(), ...
chrisl's user avatar
  • 16.3k
3 votes
Accepted

How can I replace pulseIn with interrupts?

You do not want the reading of the incoming pulse to block your program execution. This is the very key requirement: you want a non-blocking pulse reading. You can write a non-blocking replacement for ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

Faster and lighter approximate timing code without using timer

Personally I wouldn't be putting too much effort into optimizing away your timers. millis() is pretty fast - the work needed to calculate the result is done in the background anyway (by interrupts). I ...
Nick Gammon's user avatar
  • 38.4k
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

millis() function always returns same value

You never exit the ISR, hence all interrupts are disabled. Do the minimum work possible within an ISR, and have the main loop outside.
Ignacio Vazquez-Abrams's user avatar
3 votes
Accepted

Why does my servo execute an action in the beginning when the odds are almost 0?

Recognise that, despite the name, random() does not produce a truly random number. Because a computer is basically determinate, every time random() is called it produces results in a fixed sequence - ...
John Burger's user avatar
  • 1,875
3 votes
Accepted

How can I made my Arduino follow the time exactly?

Three things you can do - any combination of these is good, but the more the better: Use an RTC module to keep more accurate track of the time. Connect the Arduino to the internet and use NTP to get ...
Majenko's user avatar
  • 105k
3 votes
Accepted

Problem with Dual Safety Switches Initiating Timed Relay

The Arduino has a loop function, that runs over and over again. The Arduino has a millis function for the time. Those two combine beautifully with a state machine. Once I made a function that ...
Jot's user avatar
  • 3,246
3 votes
Accepted

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

Written and tested. I also included the red blinking led. //CONSTANTS const int BUTTON_PIN = 2; // Button const int in1 = 7; const int greenLED = 4; const int redLED = 9; //VARIABLES int ...
Gerben's user avatar
  • 11.3k
3 votes

Does millis() work inside a function?

Millis is driven by an interrupt. The reason it doesn't "work" from inside an interrupt is that you can't have two interrupts running at the same time. So when your interrupt is running the interrupt ...
Majenko's user avatar
  • 105k
3 votes

Does delay() pause the counting of millis()?

Delay uses millis. It just waits examining millis until a certain time has passed. Millis is driven by an interrupt. The only thing that stops it working is another interrupt (you can only have one ...
Majenko's user avatar
  • 105k
3 votes
Accepted

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

Declare a global (or static) boolean variable to keep track of whether the timer is running or not, as well as a variable to hold the start millis() value. When the lux drops below your threshold, ...
jose can u c's user avatar
  • 6,974
3 votes

to measure the time between the ON signal and OFF signal

Here's the catalog of errors I see at first glance: const int CHANNEL_COUNT = 3; Channel channels[CHANNEL_COUNT] = { { 2, LOW, 0, 0 }, { 3, LOW, 0, 0 }, //{ 4, LOW, 0, 0 }, /...
Majenko's user avatar
  • 105k
3 votes

Arduino Timer0 Piggyback

The Arduino core configures Timer 0 for a period of 1,024 µs. Your ISR will then be called at very regular intervals, and is a good place to have timing-related code. You just have to keep in ...
Edgar Bonet's user avatar
  • 43.3k
3 votes

Which is better millis() or timer?

It depends on your need: 1. No other activities / main power If during those 30 minutes (I guess), the MCU does not have to do anything else (except maybe handling some short interrupts), than you ...
Michel Keijzers's user avatar

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