Skip to content

Commit

Permalink
Changed method of preventing multiple rings to using a debounce timer…
Browse files Browse the repository at this point in the history
… that prevents two rings from happening in the same minute.
  • Loading branch information
Zaikur committed May 30, 2024
1 parent 2987fd3 commit 3bebbbe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/schedule/scheduleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ This file handles reading a schedule from the client, saving it to EEPROM, and c
// Constructor for ScheduleManager class that initializes the EEPROMLayoutManager, TimeManager, and RelayManager objects
ScheduleManager::ScheduleManager() : currentSchedule(4096) {
loadScheduleFromEEPROM();
lastRingTime = "";
ringInterval = 60000; // 1 minute interval
lastRingTimeMillies = 0;
}


Expand Down Expand Up @@ -74,8 +75,10 @@ String ScheduleManager::getScheduleString() {
* The `handleRing` function checks if the bell should ring and activates the relay if necessary.
*/
void ScheduleManager::handleRing() {
if (shouldRingNow()) {
unsigned long currentMillis = millis();
if (shouldRingNow() && (currentMillis - lastRingTimeMillies) >= ringInterval) {
relayManager.activateRelay();
lastRingTimeMillies = currentMillis;
}
}

Expand Down Expand Up @@ -121,8 +124,7 @@ bool ScheduleManager::shouldRingNow() {

// Look for the exact current time in today's schedule
for (JsonVariant v : times) {
if (currentTime == v.as<String>() && currentTime != lastRingTime) {
lastRingTime = currentTime;
if (currentTime == v.as<String>()) {
return true; // Time to ring the bell
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/schedule/scheduleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ScheduleManager {
JsonArray getRemainingRingTimes();
void sortScheduleTimes(DynamicJsonDocument& schedule);
String dayOfWeekStr(int day);
String lastRingTime;
void loadScheduleFromEEPROM();
DynamicJsonDocument currentSchedule;
bool validateSchedule(DynamicJsonDocument& schedule);
bool isValidTimeFormat(const String& time);
unsigned long lastRingTimeMillies;
unsigned long ringInterval;
};

0 comments on commit 3bebbbe

Please sign in to comment.