Here's an example of BlinkWithoutDelay with different on/off times. The new idea from the standard BWOD is to change the interval each time you switch from on to off and off to on. const byte ledPin =  LED_BUILTIN;// the number of the LED pin byte ledState = LOW;            unsigned long previou… Answer from cattledog on forum.arduino.cc
🌐
Kasper Kamperman
kasperkamperman.com › home › codelog › arduino programming – interval
Arduino Programming - Interval ⋆ Kasper Kamperman
April 22, 2021 - Wil namelijk een afgeleide (5v ... Arduino en zodra de netspanning weer terug is de generator stoppen. Using timer0, write a program (without using interrupts) that prints the time every 0.015 seconds. Use timer0 to ensure the 0.015 second interval as closely as ...
Discussions

interrupt - Interval timer on Arduino: Doubt about TimerOne library - Arduino Stack Exchange
I want to synchronize a timer interrupt from a button is pressed. I want to read a button state 3 seconds later from the first pulse moment (to identify long pressed button, 3 seconds for this exam... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
Interval timer on Arduino: Doubt about TimerOne library
I want to synchronize a timer interrupt from a button is pressed. I want to read a button state 3 seconds later from the first pulse moment (to identify long pressed button, 3 seconds for this example). I'm trying to do with this code: #include #define pinButton 3 #define POWER_ON_TIME 3 //Long ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
February 15, 2022
Looking for a timer interrupt library that can change intervals
Hello, looking for an answer to a hopefully simple question. I'm hoping to implement a timer interrupt that is a time multiple of a digital gate coming in from outside of the device. I can use micros() to get some good information about how long between incoming pulses but have been having ... More on forum.arduino.cc
🌐 forum.arduino.cc
5
0
April 23, 2023
How to change timer interval in interrupt routine?
Hi I am running an ESP32 Timer interrupt where I want the timer interval to change between two values every time the interrupt-routine is called. Hence, when toggle0 = true I want one interval and when toggle0 = false I want a different one. I tried setting the interval in the interrupt routine ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
October 23, 2022
🌐
GitHub
github.com › taromorimoto › interval-timer
GitHub - taromorimoto/interval-timer: A simple interval timer for Arduino
#include "IntervalTimer.h" void ... { timerA.update(); timerB.update(); } You can just make a zip of the IntervalTimer folder and add as a library in Arduino IDE....
Forked by 2 users
Languages   C++ 100.0% | C++ 100.0%
🌐
PJRC
pjrc.com › teensy › td_timing_IntervalTimer.html
Delay and Timing Functions
IntervalTimer uses interrupts to call a function at a precise timing interval. Advanced programming is required to properly use IntervalTimer, because your function runs as an interrupt.
Top answer
1 of 1
4

Don't know if I understand what you want to do. IMHO TimerOne is a good library for repetitive tasks, not for one shot events.

You should use Timer1.stop and Timer1.resume() to start the 3 seconds count whenever you press the button. But TimerOne has a known issue: when you use restart or start the ISR is fired immediatly

Here is my version of your code. It waits for button press and then starts timer. After 3 seconds pressed_check is fired and the longPressDetected flas is set. I hope I have correctly interpreted your question

#include <TimerOne.h>
#define pinButton 3
#define POWER_ON_TIME 3UL //Long press in seconds

volatile unsigned long pressed_Time = 0;
volatile unsigned long myTime_button = 0;
volatile unsigned long myTimeOn_button = 0;
volatile unsigned long myTime_pressed = 0;

volatile bool firstISR_Call = true;
volatile bool longPressDetected = false;

unsigned long holdTime = 0;
bool button_state = false;

void setup() {
    // put your setup code here, to run once:
    pinMode(pinButton, INPUT_PULLUP);
    pinMode(LED_BUILTIN, OUTPUT);

    Timer1.initialize();
    // Create an ISR that is called every POWER_ON_TIME seconds
    Timer1.attachInterrupt(pressed_check, POWER_ON_TIME * 1000000UL);
    // Stop Timer1 now
    Timer1.stop();  
    
    //Timer1.attachInterrupt(pressed_check);

    attachInterrupt(digitalPinToInterrupt(pinButton), button_act, CHANGE);
    Serial.begin(9600);
}

void loop() {
    if (longPressDetected) {
        longPressDetected = false;
        Serial.println("Long press detected");
    }
    delay(100);
}

void button_act() {
    if (!digitalRead(pinButton)) {
        myTime_button = micros(); //Flanco de bajada
        firstISR_Call = true;       // Set to true toprevent "phantom" interrupt
        longPressDetected = false;  // Clear long press detection flag
        Timer1.restart();           // Restart timer from 0 - ISSUE: this fires immediatly an interrupt
                                    // causing a so called "phantom" interrupt
    }
    else { 
      myTimeOn_button = micros() - myTime_button; 
      Timer1.stop();
    } //Flanco de subida
}

void pressed_check() {
    // Detect "phantom" interrupt
    if (firstISR_Call) {
        firstISR_Call = false;
        return;
    }
    longPressDetected = true;
    myTime_pressed = micros() - myTime_button;
    Timer1.stop();
}
🌐
Draeger IT
draeger-it.blog › startseite › interval execution made easy: timer for arduino
Interval execution made easy: Timer for Arduino - draeger-it.blog
This post is all about efficient timer programming for Arduino. Timer controlled functions play an important role in many Arduino projects, be it for precise timing or recurring tasks. I’ll show you how to use the external “timer” library to elegantly overcome these challenges. Discover an improved solution compared to traditional approaches and learn how to enrich your Arduino projects with the right time intervals...
Published   January 8, 2026
🌐
DeepBlue
deepbluembedded.com › home › blog › arduino timers [ultimate guide]
Arduino Timers [Ultimate Guide]
August 17, 2023 - Which is definitely above the required 100ms time interval. Step 2) Using the general timer equation, plug in the (TOUT value, Prescaler divider, and CLK frequency). Then solve the equation for the TicksCount value. Step 3) TicksCount = 25,000 ticks. And that’s the output of the calculation that we’ll use thereafter to program the timer module to generate the desired 100ms timer interrupt event.
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Interval timer on Arduino: Doubt about TimerOne library - Programming - Arduino Forum
February 15, 2022 - I want to synchronize a timer interrupt from a button is pressed. I want to read a button state 3 seconds later from the first pulse moment (to identify long pressed button, 3 seconds for this example). I'm trying to do with this code: #include #define pinButton 3 #define POWER_ON_TIME 3 //Long press in seconds volatile unsigned long pressed_Time = 0; volatile unsigned long myTime_button = 0; volatile unsigned long myTimeOn_button = 0; volatile unsigned long myTime_pressed = 0; u...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Looking for a timer interrupt library that can change intervals - Programming - Arduino Forum
April 23, 2023 - Hello, looking for an answer to a hopefully simple question. I'm hoping to implement a timer interrupt that is a time multiple of a digital gate coming in from outside of the device. I can use micros() to get some good information about how long between incoming pulses but have been having ...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to change timer interval in interrupt routine? - Programming - Arduino Forum
October 23, 2022 - Hi I am running an ESP32 Timer interrupt where I want the timer interval to change between two values every time the interrupt-routine is called. Hence, when toggle0 = true I want one interval and when toggle0 = false I…
🌐
Readthedocs
png-arduino-framework.readthedocs.io › timer.html
Arduino Timer — .PNG Arduino Framework 1.0 documentation
Timer(unsigned long int ms, CallBackType callback, bool isSingle); Create a Timer with time, callback and set if is single shot · void setInterval(unsigned long int ms); - Set callback interval
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Measuring time interval by using timer interrupts - Programming - Arduino Forum
August 29, 2013 - Hello, i would like to measure time intervals between specific events (lets say the time between a pin going from low to high which is triggered by a hall sensor or even a simple button). I need accuracy on this (sub ms), done some searching, and found out that probably the most accurate method for performing such tasks is enabling the timer interrupts and counting how many times it overflows.
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
7 day interval timer using millis(). General guidance sought. - General Guidance - Arduino Forum
June 5, 2019 - Hello forum. I am relatively new to microcontrollers, and definitely confused by the syntax of the Sketch language; but understanding will come with practice, and practice is what this current project is all about. I am trying to incorporate a 7 day interval timer within other code.
🌐
Forward
forward.com.au › pfod › ArduinoProgramming › TimingDelaysInArduino.html
How to code Timers and Delays in Arduino
How not to code a delay in Arduino How to write a non-blocking delay in Arduino Unsigned Long, Overflow and Unsigned Subtraction Using the millisDelay library Delay and Timer Examples – Single-Shot Delays and Repeating Timers Delay execution until condition has been true for X secs Led/Buzzer/Valve Sequencing A PinFlasher class and example Other millisDelay Library Functions
🌐
Instructables
instructables.com › circuits › arduino
Arduino Timer Interrupts : 6 Steps (with Pictures) - Instructables
November 10, 2021 - Arduino Timer Interrupts: Timer interrupts allow you to perform a task at very specifically timed intervals regardless of what else is going on in your code. In this instructable I'll explain how to setup and execute an interrupt in Clear Timer on Compare Match or CTC Mode. …
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Using a timer to trigger for certain periods between intervals - Programming - Arduino Forum
December 23, 2014 - Hi all, Say I want to switch a light on and off for a period of time at designated intervals without using delays and I would also like to change this interval later on. I am checking out the TimeAlarms "Alarm.timerRepeat" function but it has two problems: it seems to need to be placed into ...
🌐
Arduino
docs.arduino.cc › libraries › intervaltimerex
Arduino
The Arduino environment can be extended through the use of libraries. Libraries provide extra functionality for use in sketches. To use a library in a sketch, select it from Sketch > Import Library.
🌐
DeepBlue
deepbluembedded.com › home › blog › arduino timer interrupts tutorial & examples
Arduino Timer Interrupts Tutorial & Examples
August 17, 2023 - Which is definitely above the required 100ms time interval. Step 2- Using the general timer equation, plug in the (TOUT value, Prescaler divider, and CLK frequency). Then solve the equation for the TicksCount value. Step 3- TicksCount = 25,000 ticks. And that’s the output of the calculation that we’ll use thereafter to program the timer module to generate the desired 100ms timer interrupt event.