diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-02 13:27:00 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-02 14:01:08 +0200 |
commit | c181fac0ae52a94a1951214250720668bfca558e (patch) | |
tree | 8e3fd964c0320fb460681950376843317fe7df8a /Mussel | |
parent | 07bd723c11be9f26176f84183f4c6258cfde1a0e (diff) |
rewrite attitude #2 to also handle button press
Diffstat (limited to 'Mussel')
-rw-r--r-- | Mussel/Mussel.cpp | 35 | ||||
-rw-r--r-- | Mussel/Mussel.h | 5 | ||||
-rw-r--r-- | Mussel/examples/minute/minute.ino | 5 |
3 files changed, 37 insertions, 8 deletions
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<byte>( - (static_cast<unsigned long>(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 <Mussel.h> -// 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); |