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.

Filter by
Sorted by
Tagged with
13
votes
3answers
36k 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 ...
11
votes
2answers
939 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 ...
10
votes
3answers
3k 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 &...
10
votes
1answer
4k 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 ...
9
votes
6answers
32k 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 ...
9
votes
5answers
11k 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 ...
9
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?
9
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-...
9
votes
4answers
2k 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
5answers
4k 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

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 ...
7
votes
1answer
17k 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 ...
7
votes
4answers
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(...
7
votes
2answers
1k 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 ...
7
votes
3answers
2k 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 ...
6
votes
4answers
655 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 ...
6
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
2answers
9k 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 ...
5
votes
1answer
382 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); ...
5
votes
2answers
131 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 ...
5
votes
3answers
2k 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
4answers
9k 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 ...
5
votes
1answer
151 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 ...
5
votes
1answer
327 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 ...
5
votes
1answer
2k 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 ...
5
votes
1answer
127 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 ...
5
votes
1answer
232 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. (...
4
votes
1answer
319 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
350 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 ...
4
votes
1answer
8k views

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

I am trying to integrate tone() with the open source program for the Amped heart rate monitor. The error: Tone.cpp.o (symbol from plugin): In function `timer0_pin_port': (.text+0x0): multiple ...
3
votes
2answers
9k views

Reading I2C sensors with a timer interrupt

I would like to read a sensor connected via I2C using Timer3 and Arduino Mega. Values are read from the sensor in the interrupt service routine and timer3 is set to trigger an interrupt at 200Hz. ...
3
votes
4answers
16k views

How do I count time in seconds?

I have a statement: if(control > 100 && control < 130) { //count time 30 seconds and open pin1 for 3 seconds. } How can I count 30 seconds and open a pin for 3 seconds?
3
votes
2answers
4k 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
2k 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
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
145 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
2answers
635 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
1answer
141 views

Code test the duration press of the button

I wrote code that tracks the duration of a button click. It has bounce protection and frequent clicks. (Freeze) My code: bool freeze_time, btn_read, debounce; unsigned int freeze_timer, btn_timer; #...
3
votes
3answers
13k views

Stable frequency generation

I'm trying to work out the best way to generate a stable frequency with my Uno R3 I'm using interrupts to generate an approximately 40KHz frequency which drives some ICs/MOSFETs to effectively ...
3
votes
1answer
4k 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
1answer
800 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
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
873 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
1answer
117 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
821 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 ...
3
votes
1answer
3k views

TCCR1A and TCCR2A on Leonardo

I'm trying to use the TTS library for Arduino on an Arduino Micro board, which is based on the Leonardo. When I try to compile my sketch I get some errors; /libraries/TTS/TTS.cpp: In function 'void ...
3
votes
2answers
1k views

Timer2 “Clear OC2B on Compare Match” not working as expected in CTC mode

This is a follow up to my previous question. I have Timer2 in CTC mode, with TOP=OCR2A set to 150 (with a prescaler of 8, giving a 75us timing interval). The datasheet states that setting COM2B1 to ...
2
votes
3answers
5k views

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

I'm experimenting with a Nano, and trying to trigger an action around twice a day. I'm aware that it's not super accurate, but I don't really mind, as long as it's in the ballpark. I've tried using ...
2
votes
5answers
15k views

Creating a timer using a Real Time Clock with start and stop times

I have an Arduino Uno with a motor shield, as well as a DS1307 RTC that I am using to control 2 pumps. Ideally I would like to set a start time (startHour + startMinute) and end time (endHour + ...

1
2 3 4 5
7