/* * ESP32 SMART LIGHTING SYSTEM (WOKWI)
* Group Number: [Insert] | Section: [Insert]
*/
// Specific Pins
const int BUTTON_PIN = 4;
const int LDR_PIN = 34;
const int POT_PIN = 32;
const int LED_PIN = 2;
// System States
bool systemOn = false; // Requirement 1: Toggle State [cite: 25]
bool lastButtonState = HIGH;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Requirement 1: Stable behavior [cite: 30]
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// 1. BUTTON CONTROL LOGIC
bool currentButtonState = digitalRead(BUTTON_PIN);
if (lastButtonState == HIGH && currentButtonState == LOW) {
systemOn = !systemOn; // Toggle: ON -> OFF -> ON [cite: 27, 28, 29]
delay(200); // Debounce for stable behavior [cite: 30]
}
lastButtonState = currentButtonState;
// 2. READ SENSORS
int ldrValue = analogRead(LDR_PIN);
int potValue = analogRead(POT_PIN);
int brightness = 0;
// 3. HIERARCHICAL LOGIC [cite: 31, 38]
if (systemOn) {
// NIGHT MODE CHECK: Only works if LED is ON [cite: 35]
if (ldrValue < 1500) {
// BRIGHTNESS ADJUSTMENT: Only if Button is ON AND LDR < 1500
brightness = map(potValue, 0, 4095, 0, 255);
} else {
// Above 1500: LED turns OFF
brightness = 0;
}
} else {
// If System/Button is OFF, LDR and Pot do nothing [cite: 34, 42]
brightness = 0;
}
// Apply the final calculated brightness
analogWrite(LED_PIN, brightness);
// 4. SERIAL MONITOR OUTPUT [cite: 62]
Serial.print("Button: "); Serial.print(systemOn ? "ON" : "OFF");
Serial.print(" | Potentiometer: "); Serial.print(potValue);
Serial.print(" | Brightness: "); Serial.print(brightness);
Serial.print(" | LDR: "); Serial.print(ldrValue);
Serial.print(" | Night Mode: "); Serial.println(ldrValue < 1500 ? "Active" : "Inactive");
delay(100);
}