Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [timers]

Timers are the hardware in the processor used to count and time events. Use this tag for questions about the hardware timers.

10
votes
3answers
28k 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
2answers
863 views

Are function pointer assignments atomic in Arduino?

The following snippets are from TimerOne library source code: // TimerOne.h: void (*isrCallback)(); // TimerOne.cpp: ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined ...
9
votes
1answer
3k views

Setting timer3 in CTC mode - conflict with servo library

I would like to set up a timer in order to call a function 800 times per second. I'm using the Arduino Mega and Timer3 with a prescaler of 1024. To choose the prescaler factor I've considered the ...
8
votes
6answers
29k views

Very long delay() possible?

I'm trying to make an opening and closing little door that should open or close every 12 hours. I was wondering if I could just make a small looping script with a delay() for 12 hours, delay(43 200 ...
8
votes
2answers
2k views

Could someone explain this weird looking code, used to setup timers?

While looking at sketches other people have written, I occasionally come across code that looks somewhat like this: TCCR1A = 0; TCCR1B = 0; TCNT1 = 34286; TCCR1B |= (1 << CS12); TIMSK1 |= (1 &...
8
votes
5answers
10k views

How can an Arduino output a specific (i.e. 56 kHz) carrier frequency?

I'm working on a free space optics project to send data wirelessly between two points. To accomplish this I'm using an IR LED connected to an Arduino Uno that pulses with a 56 kHz carrier ...
8
votes
5answers
2k views

Can I make delayMicroseconds more accurate?

I'm trying to bit bang DMX data and that requires 4us pulses. Not having much luck with the results I'm checking to see how good the Arduino is at delaying... Seems to be pretty terrible at it. Here'...
8
votes
2answers
3k 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?
8
votes
4answers
1k views

Turn Arduino on with timer every 24h

I'm currently working on an Arduino project that sends the charge of my car battery once a day via an ESP8266 to me. During the winter I don't drive my car, and I would like to get informed when the ...
8
votes
3answers
1k views

Write PWM with only 8 bit? (Timer2)

I have a problem with the Servo library. I need to read a PPM signal and create a PWM signal of each channel. The problem is that PPM has a higher resolution than PWM so I want to use the 16 bit ...
7
votes
4answers
577 views

Programming standalone Atmega328p-PU with NE555N timer IC

I know I may sound a little idiotic to ask, but as I have seen in the internet that to program a standalone Atmel chip like that from of the Arduino's, you need 2 22pF ceramic capacitors and a 16mhz ...
7
votes
1answer
5k views

Is volatile needed when variable is accessed from > 1 ISRs, but not shared outside ISRs?

It's clearly documented that when global data is shared with an ISR and the main program, the data needs to be declared volatile in order to guarantee memory visibility (and that only suffices for 1-...
7
votes
2answers
2k views

How to update a variable in an ISR using Timers

I'm trying to check the frequency of Timer3 using a counter. The value of the counter, declared as volatile, is incremented in the ISR and every second the sum is shown in the main loop and the value ...
6
votes
1answer
12k views

The difference between “time_t” and “DateTime”

I've run into an issue trying to combine 2 different Arduino timer projects I've found online. For both projects I'm using a DS3231 RTC, but have been able use the DS1307 library just fine in my ...
6
votes
3answers
1k 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
2answers
985 views

When setting hardware timers as interrupts, should I prefer a lower prescaler value or a lower CTC?

I'm currently playing with Arduino's hardware timers, and a question came to my mind. Let me explain it a bit. Let's suppose I want a certain function to execute every 1024 clock ticks. AFAIK, I ...
6
votes
1answer
1k views

ATmega AVR frequency measurement with timer/counter

I've been rehashing some existing libraries I found on some obscure forums to measure a frequency on digital pin 5, using the ATmega328P/ATmega2560 Timer/Counter modules and an interrupt based ...
6
votes
1answer
103 views

How can I automatically get the time from the computer into an alarm clock (starts at 00:00 when plugged in)?

Okay so I did this project from Fritzing first in my circuits.io project then on the arduino itself, it all works fine just how I wanted it. But here is what I wanted to add, as you can see when you ...
6
votes
1answer
209 views

Is there a portable timer interrupt library?

I have a need for timer interrupt (for the sake of this exercise - say I need to execute something every second) I found code in many places on the internets that does this using ISR for 328/Uno. (...
5
votes
3answers
4k 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
3answers
1k views

Calculate absolute time based on cycles and RTC

I'm developing a low power data logging device. It samples sensors at the frequency of 20Hz, and also has a real time clock for absolute time (the logged time need to be exact down to ms). I think ...
5
votes
2answers
328 views

How to have precise measurements

I need to display measurements of a sensor which need to be presented on a precise time-scale. Since the loop function is not quite precise, how can i make sure that i can measure at say for example ...
5
votes
4answers
8k views

Timer2 does not work as it should

I am currently playing with Arduino timers (on UNO currently) and I am building a library that has a function that must be called every millisecond. There is plenty of code samples on Internet so I ...
4
votes
1answer
378 views

Why are Arduino timer registers not 0?

I'm trying to use the timer registers to do some timing on my Arduino Mega 2560. The output of the following code confuses me: void setup () { // Set up serial connection Serial.begin(9600); ...
4
votes
1answer
315 views

Why does timer ISR not execute?

This is my code which I have written as a library #include "avr/interrupt.h" #include "Arduino.h" #include "AllTimer.h" AllTimer::AllTimer() {} void AllTimer::dofun(void) { //TIFR1 |= _BV(...
4
votes
2answers
111 views

Trouble with Timer 0

I'm taking my first wobbly steps outside the Arduino IDE and I'm not having much success with timers / interrupts. I can set a pin to output and light an LED with registers fine but I cannot for the ...
4
votes
1answer
141 views

In Linux, how do I get this Arduino library to work

In Linux Fedora 20 Arduino I want to use the Timer library from here, from which I have downloaded the folder Timer-master.zip, but, when I try to import it, I get the Arduino message that it cannot ...
4
votes
1answer
193 views

Help managing the fallout from timer0 mode and prescaler change on ATMega2560 without modifying wiring.c

For a driving application I need the PWM mode to be phase correct and the prescaler to be 1. The issue is that the board I am working with has the load hooked up to a PWM pin reliant on timer0. The ...
4
votes
1answer
2k views

Comparing time_t values within an if statement

I am using an UNO with a RTC to control a light board. I'm using a simple control structure to decide when to turn things off and on. It's my understanding that a time_t value is just a number (number ...
3
votes
2answers
6k views

Arduino Countdown without using delay()

I am working on a project which requires countdown from 30sec to 0sec. I can't use delay() function because I am taking input from keyboard during countdown. millis() function wont help because my ...
3
votes
2answers
2k views

Arduino timers. How they work

Explain this line in simple words. TCCR0B=TCCR0B&0b11111000|0x01; As the variuos arduinos have different chips. They also have different timers and different pins which use them. Lately i want ...
3
votes
4answers
4k 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
225 views

Is there an ICRn CTC mode interrupt vector?

I can use the CTC waveform generation mode to loop to OCRnA and trigger the corresponding TIMER1_COMPA_vect interrupt. // CTC mode TCCR1A &= ~(1 << WGM10); TCCR1A &= ~(1 << WGM11);...
3
votes
2answers
1k views

If a Timer is used, disabling some PWM pins, can I still use those pins for Digital write?

It's my understanding that when a Timer is used on an Arduino (in my case the Arduino Mega), it can disable analog write on some PWM pins, which pins depends on the Timer being used. For instance on ...
3
votes
1answer
67 views

Why is no PWM signal generated using Timer 2?

I managed to work with timer 0 and 1 but am unable to generate a PWM with timer 2 in my atmega328p. This is the code: #include <avr/io.h> int main(void) { /* PD6 output */ DDRD |= (1 &...
3
votes
4answers
762 views

How to make 1 second delay more accurate?

I have written two (1 second) delay programs. The first program as follows: void setup() { Serial.begin(9600); TCCR1A = 0; TCCR1B |= (1<<CS12); TCCR1B &= ~(1<<CS11); TCCR1B &=...
3
votes
2answers
521 views

Can a timer be used in both CTC and (fast) PWM mode simultaneously?

I would like to generate a sequence of pulses of varying lengths in the range of 0-100us, so that from the start of one pulse to the start of the next there is a delay of 100us. I have set up timer0 ...
3
votes
4answers
110 views

Build a timer that starts and stops timing when 2 wires are broken

I need to build a timer that uses the breaking of copper strips to trigger its starting and stopping. The intention is to measure the speed of a rocket with this - it will break the first wire, ...
3
votes
1answer
634 views

Setting the frequency of a timer interrupt

The concept: Play a sine wave saved as a character array in PROGMEM through an 8-bit DAC connected to the PORTC pins on the Arduino Mega. This works: // File just defines an array and a size #...
3
votes
1answer
4k 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 ...
3
votes
1answer
672 views

Generating a short pulse after a delay

I am trying to use one of the hardware timers on the arduino uno to generate a short pulse some number of microseconds after receiving a pulse on an input. Currently my code looks like this: ...
3
votes
3answers
3k views

Connecting high sampling rate DAC and ADC to arduino DUE, timer consideration

I'm in the echopen association, our goal is to create a low cost, open source echographic probe. In order to do that, we use an arduino due for the first step. For doing acoustic imaging, we use ...
3
votes
2answers
2k views

Arduino Hardware timer and interrupts issue

I recently started programming with Arduino and and currently I'm making a project to read RPM and Speed of my car using Arduino Uno. So far I got everything working but the speed value does not show ...
3
votes
2answers
839 views

How do I use the timers on an Arduino Uno R3?

I want to make a simple circuit which returns the time/delay between two, consecutive loud sounds (i.e, a mic reading of ~600) coming from a speaker. I have set up and ADMP 401 microphone for this ...
3
votes
3answers
9k views

Using TIMER0_COMPB_vect

I'm trying to understand how to fully use the COMPB vector ISR of Timer0 on the ATMega328 (standard Arduino core). I realize it will make delay() and millis() not work but that's ok by me. I've setup ...
3
votes
1answer
72 views

Timer interrupt triggers too often

The following question is based on the Arduino Pro Mini (8MHz, 3.3V) I'm in process of developing (yet another) LED strip driver shield. For that I wrote a simple control loop, that adjusts the ...
3
votes
1answer
619 views

FastLed (WS2812B) and IRRemote problem

I am trying to control my WS2812B with a remote control. To archive this, I am using IRRemote library. The problem is that this two libraries doesn't work together. I did some research and found out ...
3
votes
2answers
1k views

How do I use interrupt service routines inside libraries and classes?

I'm trying to have a millis() timer run to emulate a hardware timer inside a library, but I'm having issues compiling the code. I want the callback (pseudo ISR) to be part of the class, and not use ...
3
votes
2answers
864 views

Replacing delay() with millis()

I'm currently attempting to replace delay() in my project so that i may perform other tasks while waiting on a particular event to fire. The issue I'm having, is every example i've found using ...
3
votes
1answer
700 views

Timer1 stops firing

I am trying to use interrupts to play notes on a speaker. Timer2 works fine, but after about 15 seconds, Timer1 stops firing for about 5 seconds, and then it comes back on. If I keep running the ...