#include "SAMDTimerInterrupt.h" #include "SAMD_ISR_Timer.h" SAMDTimer TaskTimer(TIMER_TC3); SAMDTimer BuzzerTimer(TIMER_TC5); 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(); initTemperature(); initTube(); pinMode(BUZZER_PIN, OUTPUT); BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzz); BuzzerTimer.disableTimer(); TaskTimer.attachInterruptInterval_MS(25, TasksHandler); TasksISRs.setInterval(PERIOD_MS, runMetronome); TasksISRs.setInterval(PERIOD_MS, readTemperature); TasksISRs.setInterval(PERIOD_MS, updateTube); } volatile bool buzzing = false; void buzz() { digitalWrite(BUZZER_PIN, buzzing ? LOW : HIGH); buzzing = !buzzing; } void stopBuzzer() { BuzzerTimer.disableTimer(); digitalWrite(BUZZER_PIN, LOW); } void runMetronome() { if (countdown > 0) { countdown -= 1; digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW); } else if (countdown == 0) { TasksISRs.setTimeout(100, stopBuzzer); BuzzerTimer.enableTimer(); } } void loop() { }