Replace delay() with ISR_Timer
This commit is contained in:
17
button.ino
17
button.ino
@@ -1,11 +1,22 @@
|
||||
volatile unsigned long lastButtonPress = millis();
|
||||
|
||||
void initButton() {
|
||||
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);
|
||||
}
|
||||
|
||||
void buttonISRstate() {
|
||||
if ((millis() - lastButtonPress) > 100) {
|
||||
if (countdown > 0)
|
||||
if (countdown < 0)
|
||||
countdown = 0;
|
||||
else
|
||||
countdown = BREAK_LENGTH;
|
||||
else if (countdown == 0)
|
||||
countdown = COUNTDOWN;
|
||||
else {
|
||||
countdown = -1;
|
||||
digitalWrite(BUTTON_LED_PIN, LOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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() {}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#include "grove_alphanumeric_display.h"
|
||||
#include "Seeed_BME280.h"
|
||||
|
||||
Seeed_Digital_Tube tube;
|
||||
BME280 sensor;
|
||||
|
||||
void initTemperature() {
|
||||
// BME280 inits Wire
|
||||
sensor.init();
|
||||
|
||||
tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR);
|
||||
tube.setBrightness(15);
|
||||
tube.setBlinkRate(BLINK_OFF);
|
||||
tube.setPoint(true,true);
|
||||
}
|
||||
|
||||
void readTemperature() {
|
||||
char temperature_str[5];
|
||||
sprintf(temperature_str, "%4u", (unsigned int) (sensor.getTemperature() * 100.0));
|
||||
tube.displayString(temperature_str);
|
||||
}
|
||||
Reference in New Issue
Block a user