const int ldrPin = 2; // Pin connected to the LDR and resistor
const int redPin = 10; // Red pin of the RGB LED
const int greenPin = 9; // Green pin of the RGB LED
void setup() {
Serial.begin(9600); // Start the Serial communication
pinMode(ldrPin, INPUT); // Initialize the LDR pin as an input
pinMode(redPin, OUTPUT); // Initialize the red LED pin as an output
pinMode(greenPin, OUTPUT); // Initialize the green LED pin as an output
}
void loop() {
int ldrStatus = digitalRead(ldrPin); // Read the digital value from the LDR
Serial.println(ldrStatus); // Print the status to the Serial Monitor
if (ldrStatus == HIGH) {
// When the LDR senses light (status HIGH), turn the green LED on and red LED off
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, LOW);
} else {
// When the LDR is in the dark (status LOW), turn the red LED on and green LED off
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
delay(500); // Wait for half a second
}