Compare commits
13 Commits
b81b52b204
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2af98d588 | ||
|
|
0c9a7a51d9 | ||
|
|
6a0a954814 | ||
|
|
d6f9bb6634 | ||
|
|
1e206b248c | ||
|
|
8594905748 | ||
|
|
814a99f733 | ||
|
|
7fe04d76cb | ||
|
|
f8878d3c37 | ||
|
|
374a72fb07 | ||
|
|
21512bc4f4 | ||
|
|
3872ece37b | ||
|
|
315aa9996c |
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
Arduino based device acting as:
|
||||
* metronome, for exercises that require timing,
|
||||
* countdown timer, for rest between exercises,
|
||||
* thermometer / hygrometer - when not uses for exercises.
|
||||
|
||||
Part list:
|
||||
* Arduino MKR 1010 (ABX00023)
|
||||
* Arduino MKR Connector Carrier, Grove compatible (ASX00007)
|
||||
* BME280 sensor (Seeed Studio SEE-11355)
|
||||
* quad, alphanumeric (14-segment), HT16K33 based display (SeedStudio SEE-14733)
|
||||
* push button with backlight (Seeed Studio SEE-13660)
|
||||
* passive buzzer (Seeed Studio SEE-17268)
|
||||
* I2C 6 port hub (Seeed Studio SEE-15856)
|
||||
* cables - as required
|
||||
All modules based on Grove connections and cables.
|
||||
|
||||
All modules are connected to Arduino through Connector Carrier:
|
||||
* buzzer - port D0,
|
||||
* button - port D5,
|
||||
* I2C hub - port TWI,
|
||||
* sensor and display - to hub (any port)
|
||||
50
bme280.ino
Normal file
50
bme280.ino
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "Seeed_BME280.h"
|
||||
|
||||
enum {
|
||||
LOWER = 0,
|
||||
HIGHER
|
||||
};
|
||||
|
||||
BME280 sensor;
|
||||
float bounds[2][SENSORS] = {};
|
||||
const float hysteresisWidth[SENSORS] = {0.03, 0.5, 0.5};
|
||||
|
||||
void initBME280() {
|
||||
sensor.init();
|
||||
}
|
||||
|
||||
void readSensors() {
|
||||
float newValue;
|
||||
|
||||
for (uint i = 0; i < SENSORS; i++) {
|
||||
switch (i) {
|
||||
case TEMPERATURE:
|
||||
newValue = sensor.getTemperature();
|
||||
break;
|
||||
|
||||
case HUMIDITY:
|
||||
newValue = sensor.getHumidity();
|
||||
break;
|
||||
|
||||
case PRESSURE:
|
||||
newValue = sensor.getPressure();
|
||||
break;
|
||||
}
|
||||
|
||||
if (newValue > bounds[HIGHER][i]) {
|
||||
sensorValue[i] = newValue;
|
||||
bounds[HIGHER][i] = newValue;
|
||||
bounds[LOWER][i] = bounds[HIGHER][i] - hysteresisWidth[i];
|
||||
if ((countdown <= 0) && (displaySensor == i)) {
|
||||
displayNeedsUpdate = true;
|
||||
}
|
||||
} else if (newValue < bounds[LOWER][i]) {
|
||||
sensorValue[i] = newValue;
|
||||
bounds[LOWER][i] = newValue;
|
||||
bounds[HIGHER][i] = bounds[LOWER][i] + hysteresisWidth[i];
|
||||
if ((countdown <= 0) && (displaySensor == i)) {
|
||||
displayNeedsUpdate = true;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
12
button.ino
12
button.ino
@@ -1,5 +1,7 @@
|
||||
volatile unsigned long lastButtonPress = millis();
|
||||
|
||||
// Debouncing using timer:
|
||||
// https://github.com/khoih-prog/TimerInterrupt/blob/master/examples/SwitchDebounce/SwitchDebounce.ino
|
||||
void initButton() {
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
// Order of ISRs matter: RISING should be invoked first
|
||||
@@ -7,15 +9,21 @@ void initButton() {
|
||||
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISRstate, RISING);
|
||||
}
|
||||
|
||||
// FIXME: runMetronome ISR should be started/stopped by button. Otherwise fraction of first PERIOD_MS is lost from countdown.
|
||||
void buttonISRstate() {
|
||||
if ((millis() - lastButtonPress) > 100) {
|
||||
if (countdown < 0)
|
||||
// TODO: enable metronome timer
|
||||
countdown = 0;
|
||||
else if (countdown == 0)
|
||||
else if (countdown == 0) {
|
||||
// TODO: restart metronome timer to align countdown
|
||||
countdown = COUNTDOWN;
|
||||
else {
|
||||
updateTube();
|
||||
} else {
|
||||
countdown = -1;
|
||||
digitalWrite(BUTTON_LED_PIN, LOW);
|
||||
updateTube();
|
||||
// TODO: disable metronome timer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
buzzer.ino
Normal file
22
buzzer.ino
Normal file
@@ -0,0 +1,22 @@
|
||||
SAMDTimer BuzzerTimer(TIMER_TC5);
|
||||
|
||||
bool attached = false;
|
||||
|
||||
void initBuzzer() {
|
||||
pinMode(BUZZER_PIN, OUTPUT);
|
||||
}
|
||||
|
||||
void buzzerISR() {
|
||||
digitalWrite(BUZZER_PIN, !digitalRead(BUZZER_PIN));
|
||||
}
|
||||
|
||||
void buzz() {
|
||||
if (attached)
|
||||
BuzzerTimer.enableTimer();
|
||||
else
|
||||
BuzzerTimer.attachInterrupt(BUZZER_FREQ *2, buzzerISR);
|
||||
}
|
||||
|
||||
void noBuzz() {
|
||||
BuzzerTimer.disableTimer();
|
||||
}
|
||||
BIN
doc/BME280 datashet, rev. 1.24.pdf
Normal file
BIN
doc/BME280 datashet, rev. 1.24.pdf
Normal file
Binary file not shown.
73
metronom.ino
73
metronom.ino
@@ -1,45 +1,82 @@
|
||||
#include "SAMDTimerInterrupt.h"
|
||||
#include "SAMD_ISR_Timer.h"
|
||||
|
||||
#define HW_TIMER_INTERVAL_MS 10
|
||||
SAMDTimer ITimer(TIMER_TC3);
|
||||
SAMD_ISR_Timer ISR_Timer;
|
||||
SAMDTimer TaskTimer(TIMER_TC3);
|
||||
SAMD_ISR_Timer TasksISRs;
|
||||
|
||||
void TimerHandler(void) { ISR_Timer.run(); }
|
||||
enum {
|
||||
TEMPERATURE = 0,
|
||||
HUMIDITY,
|
||||
PRESSURE,
|
||||
SENSORS
|
||||
};
|
||||
|
||||
const int COUNTDOWN = 300;
|
||||
const int PERIOD_MS = 1000;
|
||||
// COUNTDOWN is effectively multiplied by METRONOME_INTERVAL
|
||||
const int COUNTDOWN = 600;
|
||||
// INTERVALS and TIMESHARES given in [ms]
|
||||
const uint TASK_HANDLER_INTERVAL = 20;
|
||||
const uint METRONOME_INTERVAL = 1000;
|
||||
const uint SENSOR_INTERVAL = 1000;
|
||||
const uint SENSOR_TIMESHARE[SENSORS] = {90000, 20000, 10000};
|
||||
// FREQUENCY in [Hz]
|
||||
const uint 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
|
||||
-1 - IDLE
|
||||
0 - BEATING
|
||||
>0 - COUNTDOWN
|
||||
Unless metronome is in COUNTDOWN mode, display rotates through sensor readings.
|
||||
*/
|
||||
volatile int countdown = -1;
|
||||
// Units: temperature [C], humidity [%], pressure [P]
|
||||
volatile float sensorValue[SENSORS] = {};
|
||||
volatile uint displaySensor = TEMPERATURE;
|
||||
volatile bool displayNeedsUpdate = false;
|
||||
volatile bool tubeRotated = false;
|
||||
int tubeTimerID;
|
||||
|
||||
void TasksHandler(void) {
|
||||
TasksISRs.run();
|
||||
|
||||
if (displayNeedsUpdate || tubeRotated)
|
||||
updateTube();
|
||||
|
||||
if (tubeRotated) {
|
||||
TasksISRs.changeInterval(tubeTimerID, SENSOR_TIMESHARE[displaySensor]);
|
||||
tubeRotated = false;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
initButton();
|
||||
initBME280();
|
||||
initBuzzer();
|
||||
initTube();
|
||||
|
||||
ITimer.attachInterruptInterval_MS(10, TimerHandler);
|
||||
ISR_Timer.setInterval(PERIOD_MS, metronomeBeat);
|
||||
ISR_Timer.setInterval(PERIOD_MS, metronomeCountdown);
|
||||
readSensors();
|
||||
updateTube();
|
||||
|
||||
TaskTimer.attachInterruptInterval_MS(TASK_HANDLER_INTERVAL, TasksHandler);
|
||||
TasksISRs.setInterval(METRONOME_INTERVAL, runMetronome);
|
||||
TasksISRs.setInterval(SENSOR_INTERVAL, readSensors);
|
||||
tubeTimerID = TasksISRs.setInterval(SENSOR_TIMESHARE[displaySensor], rotateTube);
|
||||
}
|
||||
|
||||
void metronomeCountdown() {
|
||||
void runMetronome() {
|
||||
if (countdown > 0) {
|
||||
countdown -= 1;
|
||||
|
||||
digitalWrite(BUTTON_LED_PIN, countdown % 2 ? HIGH : LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void metronomeBeat() {
|
||||
if (countdown == 0)
|
||||
tone(BUZZER_PIN, 1000, 100);
|
||||
displayNeedsUpdate = true;
|
||||
} else if (countdown == 0) {
|
||||
TasksISRs.setTimeout(100, noBuzz);
|
||||
buzz();
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
|
||||
51
tube.ino
Normal file
51
tube.ino
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "grove_alphanumeric_display.h"
|
||||
|
||||
Seeed_Digital_Tube tube;
|
||||
char tubeText[5] = "";
|
||||
|
||||
void initTube() {
|
||||
// Wire initialized by BME280 sensor
|
||||
tube.setTubeType(TYPE_4, TYPE_4_DEFAULT_I2C_ADDR);
|
||||
tube.setBrightness(15);
|
||||
tube.setBlinkRate(BLINK_OFF);
|
||||
}
|
||||
|
||||
void rotateTube() {
|
||||
displaySensor = (displaySensor + 1) % SENSORS;
|
||||
tubeRotated = true;
|
||||
}
|
||||
|
||||
void updateTube() {
|
||||
char newText[5];
|
||||
bool highPoint = false, lowPoint = false;
|
||||
uint value = countdown;
|
||||
|
||||
if (countdown <= 0) {
|
||||
switch (displaySensor) {
|
||||
case TEMPERATURE:
|
||||
value = (uint) (sensorValue[TEMPERATURE] * 100);
|
||||
lowPoint = true;
|
||||
break;
|
||||
case HUMIDITY:
|
||||
// TODO: add % sign?
|
||||
value = (uint) (sensorValue[HUMIDITY]);
|
||||
break;
|
||||
case PRESSURE:
|
||||
value = (uint) (sensorValue[PRESSURE] / 100);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sprintf(newText, "%4u", value);
|
||||
|
||||
// TODO: In the current mode of display refresh (displayNeedsUpdate) it should not be necessary to check previous text
|
||||
// Maybe add LED switching for case when the strings match as a rough check before removing comparison?
|
||||
// Or LED switching when display is updated?
|
||||
if (strcmp(tubeText, newText)) {
|
||||
strcpy(tubeText, newText);
|
||||
tube.displayString(tubeText);
|
||||
tube.setPoint(highPoint, lowPoint);
|
||||
tube.display();
|
||||
}
|
||||
|
||||
displayNeedsUpdate = false;
|
||||
}
|
||||
Reference in New Issue
Block a user