/*const int ldrPin = A0; // LDR is connected to analog pin A0
const int ledPin = 9; // LED is connected to digital pin 9 (PWM enabled)
// Threshold for light intensity below which the LED should turn on
const int lightThreshold = 200;
void setup() {
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read the light intensity from the LDR
Serial.print("LDR Value: ");
Serial.println(ldrValue); // Print the LDR value to the Serial Monitor
// Check if the light intensity is below the threshold
if (ldrValue < lightThreshold) {
digitalWrite(ledPin, HIGH); // If it's dark, turn on the LED
} else {
digitalWrite(ledPin, LOW); // If it's bright enough, turn off the LED
}
delay(100); // Short delay before next reading
}*/
const int ldrPin = A0; // LDR is connected to analog pin A0
const int ledPin = 9; // LED is connected to digital pin 9 (PWM enabled)
// Threshold for light intensity below which the LED should turn on
const int lightThreshold = 200;
void setup() {
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
const int ldrValue = 50;//analogRead(ldrPin); // Read the light intensity from the LDR
Serial.print("LDR Value: ");
Serial.println(ldrValue); // Print the LDR value to the Serial Monitor
// Check if the light intensity is below the threshold
if (ldrValue < lightThreshold) {
// If it's dark, increase LED brightness
int ledBrightness = map(ldrValue, 0, lightThreshold, 255, 0);
Serial.print("ledBrightness:");
Serial.print(ledBrightness);
analogWrite(ledPin, ledBrightness); // Set the LED brightness based on the LDR value
} else {
// If it's bright enough, turn off the LED
analogWrite(ledPin, 0);
}
delay(100); // Short delay before next reading
}