From 5c377e98a60e0761c853d7b8c5dec8afc3663ef6 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 9 Mar 2025 18:06:16 +0100 Subject: rewrite more compactly --- mussel1/mussel1.ino | 22 ++++++---------------- mussel2/mussel2.ino | 22 ++++++---------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/mussel1/mussel1.ino b/mussel1/mussel1.ino index 7165b98..e623798 100644 --- a/mussel1/mussel1.ino +++ b/mussel1/mussel1.ino @@ -7,29 +7,19 @@ void setup() { Serial.begin(115200); - Serial.println(); - Serial.println("Initializing..."); - Serial.flush(); - - Serial.printf("Device ready: %s", + Serial.printf("\n\nDevice ready: %s\n", MUSSEL_ATTITUDE); } void loop() { - String msg = "good"; - if (read() >= 50) - msg = "bad"; - - Serial.printf("*** NOTIFY: %s ***\n", msg); + Serial.printf("gap: %d\n", read()); delay(1000); } byte read() { - // Get the current second (0-59) - unsigned long currentTime = millis(); - unsigned long totalSeconds = currentTime / 1000; - unsigned long currentSecond = totalSeconds % 60; - - return (byte)currentSecond; + // return 42 if current second modulo 60 is below 50, else 2 + return (byte) (((unsigned long)millis() / 1000) % 60) < 50 + ? 42 + : 2; } diff --git a/mussel2/mussel2.ino b/mussel2/mussel2.ino index 652758a..9e41448 100644 --- a/mussel2/mussel2.ino +++ b/mussel2/mussel2.ino @@ -7,29 +7,19 @@ void setup() { Serial.begin(115200); - Serial.println(); - Serial.println("Initializing..."); - Serial.flush(); - - Serial.printf("Device ready: %s", + Serial.printf("\n\nDevice ready: %s\n", MUSSEL_ATTITUDE); } void loop() { - String msg = "good"; - if (read() >= 9) - msg = "bad"; - - Serial.printf("*** NOTIFY: %s ***\n", msg); + Serial.printf("gap: %d\n", read()); delay(1000); } byte read() { - // Get second-long cyclic counter 0-11 - unsigned long currentTime = millis(); - unsigned long totalSeconds = currentTime / 1000; - unsigned long currentSecond = totalSeconds % 12; - - return (byte)currentSecond; + // return 42 if current second modulo 12 is below 9, else 2 + return (byte) (((unsigned long)millis() / 1000) % 12) < 9 + ? 42 + : 2; } -- cgit v1.2.3