#define led 14
#define pulsador 32
hw_timer_t*timer = NULL;
int t = 0;
volatile bool once = false;
volatile bool interrupt_down = false;
void IRAM_ATTR leerPulsador() {
interrupt_down = true;
once = true;
}
void IRAM_ATTR timerOn() {
t++;
}
void setup() {
Serial.begin(115200);
pinMode (pulsador, INPUT_PULLUP);
pinMode (led, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pulsador), leerPulsador, FALLING);
timer = timerBegin(80000);
timerAttachInterrupt(timer, &timerOn);
timerAlarm(timer, 80, true, 0);
t = 0;
}
void loop() {
if (interrupt_down) { //pulsado
digitalWrite(led, !digitalRead(led));
t=0;
timerStart(timer);
interrupt_down = false;
}
if (once && digitalRead(pulsador) == 1) { //soltado y 1 vez mostrar
Serial.print("time: ");
Serial.println(t);
t = 0;
timerStop(timer);
once = false;
interrupt_down = false;
}
delay(10); // this speeds up the simulation
}