/*
SMART STATUS CONTROL BOX
Hardware:
RED LED -> pin 18
YELLOW LED -> pin 19
GREEN LED -> pin 23
BUZZER -> pin 16 (active buzzer: HIGH = sound)
TOGGLE BTN -> pin 13 (change state)
RESET BTN -> pin 32 (back to NORMAL)
States:
NORMAL : Green LED ON
WARNING : Yellow LED ON
CRITICAL : Red LED ON + buzzer beeps (2 kHz tone)
Buttons are wired with INTERNAL PULL-UP:
- Not pressed = HIGH
- Pressed = LOW
*/
// --------------------- PIN DEFINITIONS ---------------------
#define RED_LED 18 // Red LED output pin
#define YELLOW_LED 19 // Yellow LED output pin
#define GREEN_LED 23 // Green LED output pin
#define BUZZER 16 // Buzzer output pin
#define BTN_TOGGLE 13 // Push button: change state
#define BTN_RESET 32 // Push button: reset to NORMAL
// Buzzer tone frequency in CRITICAL state (2 kHz)
#define TONE_CRITICAL 2000
// --------------------- STATE MACHINE -----------------------
enum State { NORMAL, WARNING, CRITICAL }; // Possible system states
State currentState = NORMAL; // Start in NORMAL state
// These remember the last **read** value of each button
// (used to detect the moment of press: HIGH -> LOW)
bool lastToggleState = HIGH;
bool lastResetState = HIGH;
// ------------------------- SETUP ---------------------------
void setup() {
// Start serial monitor for debugging and learning
Serial.begin(115200);
Serial.println("Smart Status Control Box Started!");
Serial.println("Current State: NORMAL");
// Configure LEDs and buzzer as OUTPUT pins
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
pinMode(BUZZER, OUTPUT);
// Make sure buzzer is OFF at the beginning
noTone(BUZZER);
// Configure buttons as INPUT_PULLUP
// - internal resistor pulls the pin to HIGH
// - when button is pressed, pin is connected to GND → LOW
pinMode(BTN_TOGGLE, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
// Turn on the correct LED for the initial state (NORMAL)
updateLEDs();
}
// -------------------------- LOOP ---------------------------
void loop() {
// -------- 1) READ TOGGLE BUTTON (change state) --------
bool toggleReading = digitalRead(BTN_TOGGLE);
// We only react when:
// - current reading is LOW (button pressed)
// - last reading was HIGH (previously not pressed)
if (toggleReading == LOW && lastToggleState == HIGH) {
delay(50); // small debounce delay
// Check again to be sure it wasn't just noise
if (digitalRead(BTN_TOGGLE) == LOW) {
changeState(); // go to next state
delay(200); // cooldown: ignore extra presses for 200 ms
}
}
// Save the reading for next loop iteration
lastToggleState = toggleReading;
// -------- 2) READ RESET BUTTON (back to NORMAL) --------
bool resetReading = digitalRead(BTN_RESET);
if (resetReading == LOW && lastResetState == HIGH) {
delay(50); // debounce
if (digitalRead(BTN_RESET) == LOW) {
resetToNormal(); // go back to NORMAL state
delay(200); // cooldown
}
}
// Save the reading for next loop iteration
lastResetState = resetReading;
// -------- 3) BUZZER BEHAVIOUR --------
if (currentState == CRITICAL) {
// In CRITICAL state: buzzer ON with tone
tone(BUZZER, TONE_CRITICAL);
} else {
// In other states: buzzer OFF
noTone(BUZZER);
}
// Small delay so the loop does not run too fast
delay(10);
}
// ---------------------- HELPER FUNCTIONS ------------------
// Change to the next state in the sequence:
// NORMAL -> WARNING -> CRITICAL -> NORMAL -> ...
void changeState() {
if (currentState == NORMAL) {
currentState = WARNING;
} else if (currentState == WARNING) {
currentState = CRITICAL;
} else {
currentState = NORMAL;
}
updateLEDs(); // Update the LED indicators
// Print the new state on Serial Monitor
Serial.print("State changed to: ");
switch (currentState) {
case NORMAL: Serial.println("NORMAL"); break;
case WARNING: Serial.println("WARNING"); break;
case CRITICAL: Serial.println("CRITICAL"); break;
}
}
// Force the system back to the safe NORMAL state
void resetToNormal() {
currentState = NORMAL; // set state
updateLEDs(); // update LEDs
noTone(BUZZER); // ensure buzzer is off
Serial.println("Reset to NORMAL");
}
// Turn ON only the LED that matches the current state
void updateLEDs() {
digitalWrite(GREEN_LED, (currentState == NORMAL) ? HIGH : LOW);
digitalWrite(YELLOW_LED, (currentState == WARNING) ? HIGH : LOW);
digitalWrite(RED_LED, (currentState == CRITICAL) ? HIGH : LOW);
}