Why isn't the following working? I want to push a button and that starts the timer. Once the timer is complete I want the serial monitor to print my line. I feel like this makes sense to me but I am new to arduino. I have piece this together from other code so there may be redundancies I am not seeing.
#include <Arduino.h>
//Define constants ------------------------------------
#define BTN 27
#define LED_PIN 13
//Setup variables ------------------------------------
volatile bool buttonIsPressed = false;
//Setup interrupt variables ----------------------------
volatile bool interruptCounter = false;
int totalInterrupts;
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
//Initialization ------------------------------------
void IRAM_ATTR isr() {
buttonIsPressed = true;
timerRestart(timer);
}
void IRAM_ATTR onTime() {
portENTER_CRITICAL_ISR(&timerMux);
interruptCounter = true;
portEXIT_CRITICAL_ISR(&timerMux);
}
void TimerInterruptInit() {
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTime, true);
timerAlarmWrite(timer, 10000000, true);
timerAlarmEnable(timer);
}
void setup() {
digitalWrite(LED_PIN, LOW);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN, INPUT);
attachInterrupt(BTN, isr, RISING);
TimerInterruptInit();
Serial.begin(115200);
}
//Main loop ------------------------------------
void loop() {
if (CheckForButtonPress()) {
ButtonResponse();
if (CheckForTimeDone()) {
TimeDoneResponse();
}
}
}
//Functions -------------------------------------
bool CheckForTimeDone() {
if (interruptCounter) {
return true;
interruptCounter = false;
}
else
return false;
}
bool CheckForButtonPress() {
if (buttonIsPressed == true) {
return true;
}
else
return false;
}
void TimeDoneResponse() {
Serial.println("1 second has passed since the last button press!");
timerStop(timer);
}
void ButtonResponse() {
buttonIsPressed = false;
Serial.println("Pressed!");
delay(100);
}