// Pin definitions #define LDR 2 // A0 on Seeed XIAO ESP32-C3 #define BLED 3 // Blue LED #define GLED 4 // Green LED #define RWHITE 5 // Red or White LED #define BUTTON 6 // Digital input with pull-up #define BUZZER 8 // Buzzer // Timing unsigned long lastUpdate = 0; const unsigned long interval = 100; // 100ms for responsive updates void setup() { Serial.begin(115200); pinMode(BUTTON, INPUT_PULLUP); // Button connected to GND pinMode(BLED, OUTPUT); pinMode(GLED, OUTPUT); pinMode(RWHITE, OUTPUT); pinMode(BUZZER, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - lastUpdate >= interval) { lastUpdate = currentMillis; int ldrValue = analogRead(LDR); // Read light level (0–4095) int buttonState = digitalRead(BUTTON); // LOW when pressed Serial.print("LDR: "); Serial.print(ldrValue); Serial.print(" | Button: "); Serial.println(buttonState); // Threshold for night (adjust based on real readings) bool isNight = ldrValue < 60; // LED control based on light level digitalWrite(BLED, isNight ? HIGH : LOW); digitalWrite(GLED, isNight ? HIGH : LOW); digitalWrite(RWHITE, isNight ? HIGH : LOW); digitalWrite(RWHITE, isNight ? HIGH : LOW); // Buzzer ON only while button is pressed digitalWrite(BUZZER, buttonState == LOW ? HIGH : LOW); } // Place for other non-blocking tasks }