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

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

The other answers are very good, but I want to elaborate on how micros() works. It always reads the current hardware timer (possibly TCNT0) which is constantly being updated by the hardware (in fact, ...
Nick Gammon's user avatar
  • 38.3k
20 votes
Accepted

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

You can get pretty close if you program Timer 1 directly (not through the library), and have it run with the prescaler set to 1. Ideally, you want the period of the timer in clock cycles to be: F_CPU /...
Edgar Bonet's user avatar
  • 43.3k
16 votes
Accepted

Arduino Countdown without using delay()

Background The function millis () returns an unsigned long, which is the number of milliseconds since the processor was reset (until it overflows). See: http://arduino.cc/en/Reference/Millis ...
Nick Gammon's user avatar
  • 38.3k
12 votes

Attach the Arduino ISR function to the class member

Interrupt Service Routine (ISR) outside a class Let's consider a simple use of interrupts: volatile bool switchChanged; void switchPressed () { switchChanged = true; } // end of switchPressed ...
Nick Gammon's user avatar
  • 38.3k
10 votes

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

Since the timer1 library only accepts whole numbers for the µs parameter you get a error. You could skip using the library and configure the timer directly. Or you could have a look at the source code ...
Gerben's user avatar
  • 11.3k
9 votes

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

It is not wrong to use millis() or micros() within an interrupt routine. It is wrong to use them incorrectly. The main thing here is that while you are in an interrupt routine "the clock isn't ...
Majenko's user avatar
  • 105k
9 votes
Accepted

Why are Arduino timer registers not 0?

The Arduino core does things at startup. One of those things is to configure the timers ready for PWM operation. Here's the relevant bits from init() in wiring.c: // set timer 1 prescale factor ...
Majenko's user avatar
  • 105k
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
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

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

The quoted phrase is not a warning, it is merely a statement about how things work. There's nothing intrinsically wrong with using millis() or micros() within a properly-written interrupt routine. ...
James Waldby - jwpat7's user avatar
6 votes
Accepted

How to run a timer every 12 hours (exceeding the max size of int) without an RTC?

Set the timer for every hour, which is within the limit. In your action, keep a static int, initialized to zero, which you increment each time. When it ==12, do the thing you want to do and reset your ...
Mark Smith's user avatar
  • 2,181
6 votes
Accepted

Problem with programming digispark ATtiny85 board with Arduino IDE

i=i++; //increment by one The result of the above statement is not defined. That is, it is not necessarily "i + 1". See: http://en.wikipedia.org/wiki/...
Nick Gammon's user avatar
  • 38.3k
6 votes

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

A better way to do the above average 15 kHz (or any other frequency) is with a phase accumulator scheme. There are no IF tests; on each tick of an interrupt, you add a step to an accumulator and ...
Boggyman's user avatar
  • 609
5 votes
Accepted

Attiny85 code on Attiny84

There are slight differences between registers, register names and registers bit names: WDTCR is named WDTCSR PCIE doesn't exist in GIMSK as there are two IO ports, so there are two pin change ...
KIIV's user avatar
  • 4,752
5 votes
Accepted

Turn Arduino on with timer every 24h

One approach would be use of a DS3231 (Precision Real Time Clock) module. Such modules sell on Ebay for under $1. Search for ds3231 arduino. Typically, these modules have a six-pin connector, with ...
James Waldby - jwpat7's user avatar
5 votes

How to run a timer every 12 hours (exceeding the max size of int) without an RTC?

I'm running into an issue because it takes milliseconds as an int ... No it doesn't. Look at the function prototype: int setInterval(long d, timer_callback f); Call function f every d ...
Nick Gammon's user avatar
  • 38.3k
5 votes

Programming standalone Atmega328p-PU with NE555N timer IC

No, you won't be able to build a 16 MHz clock signal using a 555 timer. That's far beyond the 555 capabilities (see N.B.). If you could squeeze out of the 555 a mere 1 MHz (which I seriously doubt), ...
Enric Blanco's user avatar
  • 2,114
5 votes
Accepted

How to make 1 second delay more accurate?

Majenko is perfectly right: you cannot expect much accuracy from an Arduino clocked by a ceramic resonator. Typical drift is of the order of 1,000 ppm, and is affected by temperature and aging. ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

Why is no PWM signal generated using Timer 2?

The output compare unit of timer 2 is connected to pins PB3 (OC2A) and PD3 (OC2B). So you have to configure those as outputs. Keep in mind that you can't choose any pin when using hardware pwm, this ...
Sim Son's user avatar
  • 1,869
5 votes
Accepted

Measure audio frequency through pulse duration

Timer 1 overflows at 255 because that's how it has been configured by the Arduino core, as it is intended to provide 8-bit PWM. If you want to use the timer in normal mode, you should undo the Arduino'...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

Is it bad practice to multitask without timing constraints

Like often, the answer is: It depends. Generally it is non-blocking programming, when you do only short things on each loop. What exactly can be considered short depends on the situation, meaning, ...
chrisl's user avatar
  • 16.3k
5 votes

Where am I going wrong in modifying Timer0 and the ISR interrupts in this example?

Don't do serial prints inside an ISR. Ever. They require interrupts to work, and inside the ISR interrupts are off. For a while the attempts to print will fill up a buffer which is emptied by another ...
Nick Gammon's user avatar
  • 38.3k
5 votes
Accepted

ISR for very fast processes, strange code found. Has ISR effect on timer behaviour?

Why isn't CTC mode used by setting WGM21 in TCCR2A? As we cannot read the author's mind, we cannot say for sure. That being said, to me, the most likely reason is that either the author was not aware ...
Edgar Bonet's user avatar
  • 43.3k
4 votes
Accepted

Why does enabling TIMSK1 mess up serial?

You've enabled interrupts for Timer 1 Compare A interrupt. Where is the interrupt handler? For example: ISR(TIMER1_COMPA_vect) { // whatever } // end of TIMER1_COMPA_vect Without that, the ...
Nick Gammon's user avatar
  • 38.3k
4 votes
Accepted

How to fix compilation error for tone() using same timer as other function

It looks like the Tone library uses Timer 2, and it looks like your other code also uses Timer 2, hence the error message. Vector 7 would be the TIMER2_COMPA_vect (Timer 2 compare "a"). Pins 3 and 11 ...
Nick Gammon's user avatar
  • 38.3k
4 votes

A confusion with setting timers

If you want to output a signal at the same speed as the system clock, I would not recommend using a timer. You can output the system clock to Port B Pin 0 by programming the CKOUT fuse on the 328P. ...
luther_stanton's user avatar
4 votes

Programming standalone Atmega328p-PU with NE555N timer IC

Yes, you could use a 555. You can also go even simpler and use an RC network. Even simpler still is to use the 1MHz RC network built in​ to the chip. The main drawback of these methods, and the 555 ...
Majenko's user avatar
  • 105k
4 votes

Programming standalone Atmega328p-PU with NE555N timer IC

The default configuration of these chips, as they come from the factory, is to use their internal 8 MHz RC oscillator downscaled at 1 MHz. So you do not need any extra oscillator to program them. ...
Edgar Bonet's user avatar
  • 43.3k
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

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