#include "SAMDTimerInterrupt.h" #include "SAMD_ISR_Timer.h" SAMDTimer TaskTimer(TIMER_TC3); SAMD_ISR_Timer TasksISRs; void TasksHandler(void) { TasksISRs.run(); } const int COUNTDOWN = 240; const int PERIOD_MS = 1000; const int BUZZER_FREQ = 300; const uint BUTTON_PIN = 6; const uint BUTTON_LED_PIN = 5; const uint BUZZER_PIN = 0; /* Metronome state is expressed by countdown: * -1 - IDLE * 0 - BEATING * >0 - COUNTDOWN */ volatile int countdown = -1; volatile float temperature = 0.0; void setup() { initButton(); initBME280(); initBuzzer(); initTube(); TaskTimer.attachInterruptInterval_MS(20, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); TasksISRs.setInterval(PERIOD_MS, updateTube); } void runMetronome() { if (countdown > 0) { countdown -= 1; digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { TasksISRs.setTimeout(100, noBuzz); buzz(); } } void loop() {}