From c181fac0ae52a94a1951214250720668bfca558e Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Wed, 2 Apr 2025 13:27:00 +0200 Subject: rewrite attitude #2 to also handle button press --- Mussel/Mussel.cpp | 35 +++++++++++++++++++++++++++++------ Mussel/Mussel.h | 5 +++++ Mussel/examples/minute/minute.ino | 5 +++-- 3 files changed, 37 insertions(+), 8 deletions(-) (limited to 'Mussel') diff --git a/Mussel/Mussel.cpp b/Mussel/Mussel.cpp index b8cc4da..7cd7cf4 100644 --- a/Mussel/Mussel.cpp +++ b/Mussel/Mussel.cpp @@ -50,6 +50,16 @@ Mussel::Mussel(int attitude, uint8_t pin, uint8_t type) /// Setup function void Mussel::begin() { switch(_attitude) { + case 1: + _boolState = HIGH; + + // reset timer + _time = millis(); + + // use INPUT_PULLDOWN to signal when the button is held down + // (not PULLUP which signals when it is released) + pinMode(_pin, INPUT_PULLDOWN); + break; #ifdef DHT_H case 3: mussel_dht.begin(); @@ -82,7 +92,7 @@ String Mussel::desc() { switch(_attitude) { case 1: - _str = "closed 10 seconds every 50 seconds"; + _str = "closed 10 seconds every 50 seconds and on button push"; break; case 2: _str = "closed 4 seconds every 8 seconds"; @@ -119,11 +129,24 @@ byte Mussel::read() { switch(_attitude) { case 1: - // 42 if current second modulo 60 is below 50, else 2 - _byte = static_cast( - (static_cast(millis() / 1000) % 60) < 50 - ? 42 - : 2); + if (digitalRead(_pin) + // TODO: account for rollover + or (millis() - _time) > MUSSEL_NORMAL_PACE * 1000 + ) { + _byte = 2; + _boolState = HIGH; + + // reset timer + _time = millis(); + } else if (_boolState == HIGH + and (millis() - _time) > MUSSEL_STRESS_PACE * 1000 + ) { + _byte = 42; + _boolState = LOW; + + // reset timer + _time = millis(); + } break; case 2: // 42 if current second modulo 12 is below 9, else 2 diff --git a/Mussel/Mussel.h b/Mussel/Mussel.h index 3cce885..79f5017 100644 --- a/Mussel/Mussel.h +++ b/Mussel/Mussel.h @@ -15,6 +15,11 @@ #define Mussel_h #include "Arduino.h" +// seconds +#define MUSSEL_NORMAL_PACE 50U +#define MUSSEL_STRESS_PACE 10U + +// milliseconds #define MUSSEL_DEBOUNCE_DELAY 50U class Mussel { diff --git a/Mussel/examples/minute/minute.ino b/Mussel/examples/minute/minute.ino index 928537c..4bf59ce 100644 --- a/Mussel/examples/minute/minute.ino +++ b/Mussel/examples/minute/minute.ino @@ -1,7 +1,8 @@ #include -// instantiate with attitude #2 -Mussel mussel(2); +// instantiate with attitude #2, +// and pin 4 connected to a button +Mussel mussel(2, 4); void setup() { Serial.begin(115200); -- cgit v1.2.3