// Constants
const int ldrPin = A0; // Analog pin connected to the LDR
const int ledPin = 9; // Digital pin connected to the LED
const int threshold = 500; // Threshold value for light
void setup() {
// Initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// Initialize serial communication for debugging:
Serial.begin(9600);
}
void loop() {
// Read the value from the LDR:
int ldrValue = analogRead(ldrPin);
// Print the LDR value to the serial monitor:
Serial.println(ldrValue);
// If the LDR value is less than the threshold, turn on the LED:
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH);
} else {
// Otherwise, turn off the LED:
digitalWrite(ledPin, LOW);
}
// Wait for a short period before taking the next reading:
delay(100);
}