diff --git a/src/Output.h b/src/Output.h index e9c4aba..e870231 100644 --- a/src/Output.h +++ b/src/Output.h @@ -27,14 +27,15 @@ protected: unsigned long stateMillis = 0; - void _write(const bool state) { - if (state != get()) { + void _write(const bool wanted) { + const auto current = get(); + if (wanted != current) { if (logState) { - Serial.printf("[RELAY] \"%s\" = %s\n", name.c_str(), state ? "ON" : "OFF"); + Serial.printf("[RELAY] \"%s\" = %s\n", name.c_str(), wanted ? "ON" : "OFF"); } stateMillis = millis(); } - digitalWrite(pin, state ^ inverted ? HIGH : LOW); + digitalWrite(pin, wanted ^ inverted ? HIGH : LOW); } void _applyInitial() { @@ -73,19 +74,21 @@ public: } void cycle(const unsigned long onMillis_, const unsigned long offMillis_, const unsigned long onCount_ = -1) { + set(false); // this sets onCount=0, so do this first this->onMillis = onMillis_; this->offMillis = offMillis_; this->onCount = onCount_; - set(false); } void loop() { - if (get()) { - if (onMillis > 0 && millis() - stateMillis > onMillis) { + const auto status = get(); + const auto ageMillis = millis() - stateMillis; + if (status) { + if (onMillis > 0 && ageMillis >= onMillis) { _write(false); } } else { - if (offMillis > 0 && millis() - stateMillis > offMillis && onCount != 0) { + if (offMillis > 0 && ageMillis >= offMillis && onCount != 0) { _write(true); if (onCount > 0) { onCount--; diff --git a/src/wifi.cpp b/src/wifi.cpp index f76b0f3..0778139 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -30,6 +30,7 @@ void wifiConnect() { const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID); const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, true, true); + Serial.printf("[WiFi] Connecting: \"%s\"\n", wifiSSID.c_str()); WiFi.hostname(hostname); WiFi.begin(wifiSSID.c_str(), wifiPass.c_str()); wifiLast = max(1UL, millis());