#define LDR_PIN 32 // LDR connected to A0
#define POT_PIN 35 // Potentiometer connected to A1
#define BUTTON_PIN 4 // Push Button connected to D15
#define LED1_PIN 12 // LED1 connected to D5
#define LED2_PIN 14 // LED2 connected to D18
#define LED3_PIN 13 // LED3 connected to D19
int ledState = LOW; // Current state of LEDs
bool ldrWorking = true; // Assume LDR is working initially
bool buttonPressed = false; // Track button state for manual mode
void setup() {
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
Serial.begin(115200); // Debugging purposes
}
void loop() {
// Read LDR value
int ldrValue = analogRead(LDR_PIN);
int potValue = analogRead(POT_PIN); // Read potentiometer value (0-4095)
// Map potentiometer value to PWM range (0-255)
int brightness = map(potValue, 0, 4095, 0, 255);
// Check if LDR is functional
if (ldrValue < 100 || ldrValue > 4000) {
ldrWorking = false; // Assume LDR is not working if values are extreme
} else {
ldrWorking = true;
}
// Check button state
if (digitalRead(BUTTON_PIN) == LOW) {
delay(50); // Debounce
if (!buttonPressed) {
buttonPressed = true;
ledState = !ledState; // Toggle LEDs
}
} else {
buttonPressed = false;
}
// Control LEDs
if (ldrWorking) {
if (ldrValue < 600) { // Daytime threshold
ledState = LOW; // Turn off LEDs
} else { // Nighttime threshold
ledState = HIGH; // Turn on LEDs
}
}
// Set LED brightness and state
analogWrite(LED1_PIN, ledState ? brightness : 0);
analogWrite(LED2_PIN, ledState ? brightness : 0);
analogWrite(LED3_PIN, ledState ? brightness : 0);
// Debugging
Serial.print("LDR: "); Serial.print(ldrValue);
Serial.print(" | POT: "); Serial.print(potValue);
Serial.print(" | LDR Working: "); Serial.print(ldrWorking);
Serial.print(" | LEDs: "); Serial.println(ledState ? "ON" : "OFF");
delay(100);
}