Replace delay() with ISR_Timer
This commit is contained in:
53
metronom.ino
53
metronom.ino
@@ -5,44 +5,41 @@
|
||||
SAMDTimer ITimer(TIMER_TC3);
|
||||
SAMD_ISR_Timer ISR_Timer;
|
||||
|
||||
void TimerHandler(void) {
|
||||
ISR_Timer.run();
|
||||
}
|
||||
void TimerHandler(void) { ISR_Timer.run(); }
|
||||
|
||||
const int COUNTDOWN = 300;
|
||||
const int PERIOD_MS = 1000;
|
||||
|
||||
const uint BUTTON_PIN = 6;
|
||||
const uint BUTTON_LED_PIN = 5;
|
||||
const uint BUZZER_PIN = 0;
|
||||
|
||||
const uint BUTTON_LED_PIN = 5;
|
||||
const uint BUTTON_PIN = 6;
|
||||
|
||||
const int BREAK_LENGTH = 300000;
|
||||
const int BREAK_STEP = 2000;
|
||||
|
||||
volatile int countdown = 0;
|
||||
int step;
|
||||
/* Metronome state is expressed by countdown:
|
||||
* -1 - IDLE
|
||||
* 0 - BEATING
|
||||
* >0 - COUNTDOWN
|
||||
*/
|
||||
volatile int countdown = -1;
|
||||
|
||||
void setup() {
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
// Order of ISRs matter: RISING should be invoked first
|
||||
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRtime, CHANGE);
|
||||
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING);
|
||||
initButton();
|
||||
|
||||
initTemperature();
|
||||
ITimer.attachInterruptInterval_MS(10, TimerHandler);
|
||||
ISR_Timer.setInterval(1000, readTemperature);
|
||||
ISR_Timer.setInterval(PERIOD_MS, metronomeBeat);
|
||||
ISR_Timer.setInterval(PERIOD_MS, metronomeCountdown);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
void metronomeCountdown() {
|
||||
if (countdown > 0) {
|
||||
step = min(BREAK_STEP, countdown);
|
||||
countdown -= step;
|
||||
|
||||
digitalWrite(BUTTON_LED_PIN, HIGH);
|
||||
delay(min(BREAK_STEP/2, step));
|
||||
digitalWrite(BUTTON_LED_PIN, LOW);
|
||||
delay(max(step-BREAK_STEP/2, 0));
|
||||
} else {
|
||||
tone(BUZZER_PIN, 1000, 100);
|
||||
delay(1000);
|
||||
countdown -= 1;
|
||||
|
||||
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void metronomeBeat() {
|
||||
if (countdown == 0)
|
||||
tone(BUZZER_PIN, 1000, 100);
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
|
||||
Reference in New Issue
Block a user