Replace tone() with custom timer
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
volatile unsigned long lastButtonPress = millis();
|
volatile unsigned long lastButtonPress = millis();
|
||||||
|
|
||||||
|
// Debouncing na timerze:
|
||||||
|
// https://github.com/khoih-prog/TimerInterrupt/blob/master/examples/SwitchDebounce/SwitchDebounce.ino
|
||||||
void initButton() {
|
void initButton() {
|
||||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||||
// Order of ISRs matter: RISING should be invoked first
|
// Order of ISRs matter: RISING should be invoked first
|
||||||
|
|||||||
38
metronom.ino
38
metronom.ino
@@ -1,15 +1,16 @@
|
|||||||
#include "SAMDTimerInterrupt.h"
|
#include "SAMDTimerInterrupt.h"
|
||||||
#include "SAMD_ISR_Timer.h"
|
#include "SAMD_ISR_Timer.h"
|
||||||
|
|
||||||
#define HW_TIMER_INTERVAL_MS 10
|
SAMDTimer TaskTimer(TIMER_TC3);
|
||||||
SAMDTimer ITimer(TIMER_TC3);
|
SAMDTimer BuzzerTimer(TIMER_TC5);
|
||||||
SAMD_ISR_Timer ISR_Timer;
|
SAMD_ISR_Timer TasksISRs;
|
||||||
|
|
||||||
void TimerHandler(void) { ISR_Timer.run(); }
|
void TasksHandler(void) { TasksISRs.run(); }
|
||||||
|
|
||||||
|
|
||||||
const int COUNTDOWN = 240;
|
const int COUNTDOWN = 240;
|
||||||
const int PERIOD_MS = 1000;
|
const int PERIOD_MS = 1000;
|
||||||
|
const int BUZZER_FREQ = 300;
|
||||||
|
|
||||||
const uint BUTTON_PIN = 6;
|
const uint BUTTON_PIN = 6;
|
||||||
const uint BUTTON_LED_PIN = 5;
|
const uint BUTTON_LED_PIN = 5;
|
||||||
@@ -28,10 +29,25 @@ void setup() {
|
|||||||
initTemperature();
|
initTemperature();
|
||||||
initTube();
|
initTube();
|
||||||
|
|
||||||
ITimer.attachInterruptInterval_MS(10, TimerHandler);
|
pinMode(BUZZER_PIN, OUTPUT);
|
||||||
ISR_Timer.setInterval(PERIOD_MS, runMetronome);
|
BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzz);
|
||||||
ISR_Timer.setInterval(PERIOD_MS, readTemperature);
|
BuzzerTimer.disableTimer();
|
||||||
ISR_Timer.setInterval(PERIOD_MS, updateTube);
|
|
||||||
|
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() {
|
void runMetronome() {
|
||||||
@@ -40,8 +56,10 @@ void runMetronome() {
|
|||||||
|
|
||||||
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
|
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
|
||||||
} else if (countdown == 0) {
|
} else if (countdown == 0) {
|
||||||
//tone(BUZZER_PIN, 300, 100);
|
TasksISRs.setTimeout(100, stopBuzzer);
|
||||||
|
BuzzerTimer.enableTimer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {}
|
void loop() {
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user