#define LDR_PIN A0 // Photoresistor connected to analog pin A0
#define LED_PIN 9 // Main LED connected to digital PWM pin 9
void setup() {
pinMode(LDR_PIN, INPUT); // Set LDR pin as input
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial monitor for debugging
}
void loop() {
// Read ambient light level from LDR
int ldrValue = analogRead(LDR_PIN);
int ledBrightness = map(ldrValue, 0, 1023, 255, 0); // Map LDR value to LED brightness
// Adjust main LED brightness based on ambient light
analogWrite(LED_PIN, ledBrightness);
// Limited debug output to reduce delay in Serial communication
if (millis() % 1000 == 0) { // Print only every 1 second
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" | LED Brightness: ");
Serial.println(ledBrightness);
}
delay(10); // Reduced delay for faster response
}