Skip to content

Commit 1c7e076

Browse files
heyisulaonelicodes
andcommitted
Refactor buzzer logic and improve logging
Reworked buzzer state management for independent control of two buzzers, replaced verbose logging with concise messages to reduce loop lag, and improved Firebase initialization feedback. Updated main loop to conditionally run AutoLighting only when manual override is off, and added concise telemetry logging. Also switched Firebase library include to Firebase_ESP_Client. Co-Authored-By: Oneli Wijesuriya <247418039+onelicodes@users.noreply.github.com>
1 parent 1df7196 commit 1c7e076

7 files changed

Lines changed: 117 additions & 94 deletions

File tree

ESP32-S3-Main/platformio.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ lib_deps =
2626
electroniccats/MPU6050@^1.4.4
2727
teckel12/NewPing@^1.9.7
2828
denyssene/SimpleKalmanFilter@^0.1.0
29+
build_type = debug
2930
build_flags =
3031
-DARDUINO_USB_MODE=0
3132
-DARDUINO_USB_CDC_ON_BOOT=0
33+
-DCORE_DEBUG_LEVEL=3
3234
monitor_filters =
3335
esp32_exception_decoder
3436
time
37+
colorize

ESP32-S3-Main/src/actuators/le0066.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ void LEDArray::controlFromFirebase(int lightadj_left, int lightadj_right) {
5858
analogWrite(leftPin, leftPWM);
5959
analogWrite(rightPin, rightPWM);
6060

61-
// Log brightness changes
62-
Serial.println("┌─────────────────────────");
63-
Serial.print("│ LED Brightness Control:");
64-
Serial.println();
65-
Serial.print("│ Left: ");
66-
Serial.print(lightadj_left);
67-
Serial.print("% (PWM: ");
68-
Serial.print(leftPWM);
69-
Serial.print(") ");
70-
Serial.println(leftPWM == 0 ? "OFF" : "ON");
71-
Serial.print("│ Right: ");
72-
Serial.print(lightadj_right);
73-
Serial.print("% (PWM: ");
74-
Serial.print(rightPWM);
75-
Serial.print(") ");
76-
Serial.println(rightPWM == 0 ? "OFF" : "ON");
77-
Serial.println("└─────────────────────────");
61+
// Concise one-line log to avoid loop lag
62+
Serial.printf("LEDs: L:%d%% R:%d%%\n", lightadj_left, lightadj_right);
7863
}

ESP32-S3-Main/src/actuators/sfm27.cpp

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ void Buzzer::begin() {
1313
pinMode(buzzer1Pin, OUTPUT);
1414
pinMode(buzzer2Pin, OUTPUT);
1515

16-
patternActive = false;
17-
currentStep = 0;
18-
nextStepTime = 0;
16+
buzzer1PatternActive = false;
17+
buzzer2PatternActive = false;
18+
lastBuzzer1Ring = false;
19+
lastBuzzer2Ring = false;
1920

2021
stop();
2122
}
@@ -24,15 +25,21 @@ void Buzzer::setVolume(int vol) {
2425
volume = constrain(vol, 0, 100);
2526
}
2627

27-
void Buzzer::drive(uint16_t duration_ms, uint16_t freq) {
28+
void Buzzer::drive(uint16_t buzzerNum, uint16_t freq) {
2829
if (volume == 0) {
2930
stop();
3031
return;
3132
}
3233

33-
// Max loudness: Push-pull (one pin HIGH, one pin LOW)
34-
digitalWrite(buzzer1Pin, HIGH);
35-
digitalWrite(buzzer2Pin, LOW);
34+
// Drive pins independently
35+
if (buzzerNum == 1) {
36+
digitalWrite(buzzer1Pin, HIGH);
37+
Serial.println(" -> B1 HIGH");
38+
}
39+
else if (buzzerNum == 2) {
40+
digitalWrite(buzzer2Pin, HIGH);
41+
Serial.println(" -> B2 HIGH");
42+
}
3643
}
3744

3845
void Buzzer::stop() {
@@ -41,36 +48,63 @@ void Buzzer::stop() {
4148
}
4249

4350
void Buzzer::playTone(BuzzerTone tone) {
44-
patternActive = true;
45-
patternStartTime = millis();
46-
currentStep = 0;
47-
51+
startPattern(1, tone);
52+
}
53+
54+
void Buzzer::startPattern(int buzzerNum, BuzzerTone tone) {
55+
int steps = 1;
4856
switch (tone) {
49-
case TONE_CONFIRM: totalSteps = 1; break; // 1 beep
50-
case TONE_WARNING: totalSteps = 3; break; // Beep, Sil, Beep
51-
case TONE_ERROR: totalSteps = 5; break; // Beep, Sil, Beep, Sil, Beep
52-
case TONE_EMERGENCY: totalSteps = 19; break; // Rapid pulses
57+
case TONE_CONFIRM: steps = 1; break;
58+
case TONE_WARNING: steps = 3; break;
59+
case TONE_ERROR: steps = 5; break;
60+
case TONE_EMERGENCY: steps = 19; break;
61+
}
62+
63+
if (buzzerNum == 1) {
64+
Serial.println(" -> B1 Pattern Start");
65+
buzzer1PatternActive = true;
66+
buzzer1Step = 0;
67+
buzzer1TotalSteps = steps;
68+
buzzer1NextStepTime = millis();
69+
} else {
70+
Serial.println(" -> B2 Pattern Start");
71+
buzzer2PatternActive = true;
72+
buzzer2Step = 0;
73+
buzzer2TotalSteps = steps;
74+
buzzer2NextStepTime = millis();
5375
}
54-
nextStepTime = millis();
5576
}
5677

5778
void Buzzer::update() {
58-
if (!patternActive) return;
59-
60-
if (millis() >= nextStepTime) {
61-
// Step logic: Even steps are ON, Odd steps are OFF
62-
if (currentStep % 2 == 0) {
63-
drive(0, 0); // Sound ON
64-
nextStepTime = millis() + 150; // Duration 150ms
79+
// Update Buzzer 1
80+
if (buzzer1PatternActive && millis() >= buzzer1NextStepTime) {
81+
if (buzzer1Step % 2 == 0) {
82+
drive(1, 0);
83+
buzzer1NextStepTime = millis() + 150;
6584
} else {
66-
stop(); // Sound OFF
67-
nextStepTime = millis() + 100; // Gap 100ms
85+
digitalWrite(buzzer1Pin, LOW);
86+
buzzer1NextStepTime = millis() + 100;
87+
}
88+
buzzer1Step++;
89+
if (buzzer1Step >= buzzer1TotalSteps) {
90+
digitalWrite(buzzer1Pin, LOW);
91+
buzzer1PatternActive = false;
6892
}
93+
}
6994

70-
currentStep++;
71-
if (currentStep >= totalSteps) {
72-
stop();
73-
patternActive = false;
95+
// Update Buzzer 2
96+
if (buzzer2PatternActive && millis() >= buzzer2NextStepTime) {
97+
if (buzzer2Step % 2 == 0) {
98+
drive(2, 0);
99+
buzzer2NextStepTime = millis() + 150;
100+
} else {
101+
digitalWrite(buzzer2Pin, LOW);
102+
buzzer2NextStepTime = millis() + 100;
103+
}
104+
buzzer2Step++;
105+
if (buzzer2Step >= buzzer2TotalSteps) {
106+
digitalWrite(buzzer2Pin, LOW);
107+
buzzer2PatternActive = false;
74108
}
75109
}
76110
}
@@ -79,47 +113,34 @@ void Buzzer::controlFromFirebase(bool buzzer01ring, bool buzzer02ring, int buzze
79113
if (buzzersound != lastVolume) {
80114
lastVolume = buzzersound;
81115
setVolume(buzzersound);
82-
83-
Serial.print("Buzzer volume set to: ");
84-
Serial.print(buzzersound);
85-
Serial.println("%");
116+
Serial.printf("Buzzer volume: %d%%\n", buzzersound);
86117
}
87118

88-
if (buzzer01ring) {
89-
if (!buzzer1Active) {
90-
buzzer1Active = true;
91-
playTone(TONE_CONFIRM);
92-
}
93-
} else {
94-
buzzer1Active = false;
119+
if (buzzer01ring && !lastBuzzer1Ring) {
120+
startPattern(1, TONE_CONFIRM);
95121
}
122+
lastBuzzer1Ring = buzzer01ring;
96123

97-
if (buzzer02ring) {
98-
if (!buzzer2Active) {
99-
buzzer2Active = true;
100-
playTone(TONE_EMERGENCY);
101-
}
102-
} else {
103-
buzzer2Active = false;
124+
if (buzzer02ring && !lastBuzzer2Ring) {
125+
startPattern(2, TONE_EMERGENCY);
104126
}
127+
lastBuzzer2Ring = buzzer02ring;
105128
}
106129

107130
void Buzzer::singleBeep() {
108-
playTone(TONE_CONFIRM);
131+
startPattern(1, TONE_CONFIRM);
109132
}
110133

111134
void Buzzer::doubleBeep() {
112-
patternActive = true;
113-
patternStartTime = millis();
114-
currentStep = 0;
115-
totalSteps = 3; // On, Off, On
116-
nextStepTime = millis();
135+
buzzer1PatternActive = true;
136+
buzzer1Step = 0;
137+
buzzer1TotalSteps = 3;
138+
buzzer1NextStepTime = millis();
117139
}
118140

119141
void Buzzer::tripleBeep() {
120-
patternActive = true;
121-
patternStartTime = millis();
122-
currentStep = 0;
123-
totalSteps = 5; // On, Off, On, Off, On
124-
nextStepTime = millis();
142+
buzzer1PatternActive = true;
143+
buzzer1Step = 0;
144+
buzzer1TotalSteps = 5;
145+
buzzer1NextStepTime = millis();
125146
}

ESP32-S3-Main/src/actuators/sfm27.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@ class Buzzer {
1919
void drive(uint16_t cycles, uint16_t freq);
2020

2121
// Buzzer state tracking
22-
bool buzzer1Active;
23-
bool buzzer2Active;
22+
bool buzzer1PatternActive;
23+
bool buzzer2PatternActive;
24+
int buzzer1Step;
25+
int buzzer2Step;
26+
unsigned long buzzer1NextStepTime;
27+
unsigned long buzzer2NextStepTime;
28+
int buzzer1TotalSteps;
29+
int buzzer2TotalSteps;
2430
int lastVolume;
2531

32+
// Firebase state
33+
bool lastBuzzer1Ring;
34+
bool lastBuzzer2Ring;
35+
2636
public:
2737
Buzzer();
2838
void begin();
@@ -39,14 +49,7 @@ class Buzzer {
3949
void controlFromFirebase(bool buzzer01ring, bool buzzer02ring, int buzzersound);
4050

4151
private:
42-
unsigned long patternStartTime;
43-
unsigned long nextStepTime;
44-
int currentStep;
45-
int totalSteps;
46-
bool patternActive;
47-
48-
// Pattern definitions
49-
void startPattern(int type);
52+
void startPattern(int buzzerNum, BuzzerTone tone);
5053
};
5154

5255
#endif

ESP32-S3-Main/src/communication/firebase_manager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ void FirebaseManager::begin(const char* apiKey,
77
const char* userEmail,
88
const char* userPassword) {
99

10+
Serial.println(" -> Config API...");
1011
config.api_key = apiKey;
1112
config.database_url = databaseUrl;
1213

14+
Serial.println(" -> Config Auth...");
1315
auth.user.email = userEmail;
1416
auth.user.password = userPassword;
1517

18+
Serial.println(" -> Calling Firebase.begin...");
1619
Firebase.begin(&config, &auth);
20+
21+
Serial.println(" -> Setting reconnect...");
1722
Firebase.reconnectWiFi(true);
1823

1924
Serial.println("Firebase Connected!");

ESP32-S3-Main/src/communication/firebase_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define FIREBASE_MANAGER_H
33

44
#include <Arduino.h>
5-
#include <FirebaseESP32.h>
5+
#include <Firebase_ESP_Client.h>
66

77
struct FirebaseTxData {
88
// Sensor readings - SEND ONLY

ESP32-S3-Main/src/main.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void setup() {
9191
Serial.println("Initiated.");
9292

9393
Serial.print("Initializing Firebase...");
94+
Serial.printf(" [Free Heap: %d] ", ESP.getFreeHeap());
9495
firebase.begin(FIREBASE_API_KEY, FIREBASE_DATABASE_URL, FIREBASE_USER_EMAIL, FIREBASE_USER_PASSWORD);
9596
Serial.println("Initiated.");
9697

@@ -156,11 +157,6 @@ void setup() {
156157
}
157158

158159
void loop() {
159-
// Background Tasks
160-
wifi.update();
161-
lightSensor.update();
162-
autoLighting->update();
163-
motion.update();
164160
buzzer.update();
165161

166162
// UART Acknowledgment Check
@@ -181,12 +177,20 @@ void loop() {
181177
if (millis() - lastFirebaseRx >= 100) { // Check every 100ms
182178
lastFirebaseRx = millis();
183179
if (firebase.receiveData(rx)) {
184-
// Actuator real-time control
180+
// Actuator real-time control (Overrides AutoLighting)
185181
buzzer.controlFromFirebase(rx.buzzer01ring, rx.buzzer02ring, rx.buzzersound);
186182
leds.controlFromFirebase(rx.lightadj_left, rx.lightadj_right);
187183
}
188184
}
189185

186+
// WiFi and AutoLighting updates (Moved to governed intervals)
187+
wifi.update();
188+
189+
// Only run AutoLighting if Firebase manual override is OFF (0 brightness)
190+
if (rx.lightadj_left == 0 && rx.lightadj_right == 0) {
191+
autoLighting->update();
192+
}
193+
190194
// Real-time sensor monitoring (Mode independent)
191195
heartRate.monitorHeartRate(rx.heartrate_start, currentHR, currentSpO2);
192196
ultrasonic.monitorUltrasonic(rx.ultrasonic_start, usCenter, usLeft, usRear, usRight);
@@ -217,6 +221,10 @@ void loop() {
217221
tx.compartment = currentCompartment;
218222

219223
firebase.sendData(tx);
224+
225+
// Concise telemetry log to save time
226+
Serial.printf("Bat: %.2fV | HR: %d | L:%d | C:%s\n",
227+
tx.voltage / 1000.0f, tx.hr, tx.lightlevel, tx.colour.c_str());
220228
}
221229

222230
// Update Menu and UI
@@ -269,6 +277,4 @@ void loop() {
269277
default:
270278
break;
271279
}
272-
273-
delay(10);
274280
}

0 commit comments

Comments
 (0)