Don't know if I can't verify it now, but this code should workunderstand what you want to do.
Just addIMHO TimerOne is a variablegood library for repetitive tasks, not for one shot events.
You should use Timer1.stop and Timer1.resume() to trackstart the pressure3 seconds count whenever you press the button.
But TimerOne has a known issue: when you use restart or start time in microsthe 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 33UL //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 unsigned longbool pressStartfirstISR_Call = 0;true;
volatile bool //longPressDetected EDIT= false;
volatile bool longPress = 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("myTime_pressed""Long press detected");
Serial.println}
delay(myTime_pressed100);
}
void button_act() {
if (!digitalRead(pinButton)) {
pressStartmyTime_button = micros(); //Flanco EDITde bajada
Timer1.attachInterrupt(pressed_check,firstISR_Call POWER_ON_TIME= *true; 1000000); // Set to true toprevent "phantom" interrupt
} longPressDetected = false; // Clear long press detection flag
else { Timer1.detachInterruptrestart(); }
}
void button_act() {
if (!digitalRead(pinButton)) { // Restart timer from 0 - ISSUE: this fires immediatly an interrupt
myTime_button = micros(); //Flanco decausing bajadaa so called "phantom" interrupt
}
else { myTimeOn_button = micros(); - myTime_button; } //Flanco de subida
}
void pressed_check() {
// EDITDetect "phantom" interrupt
if (firstISR_Call) {
firstISR_Call = false;
return;
}
longPressDetected = true;
myTime_pressed = micros() - pressStart;myTime_button;
Timer1.stop();
longPress = true;
}