const int ldrPin = A0; // Pin connected to the LDR
const int redPin = 9; // Red pin of the RGB LED
const int greenPin = 10; // Green pin of the RGB LED
const int bluePin = 11; // Blue pin of the RGB LED
void setup() {
pinMode(ldrPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read the value from the LDR
int redValue, greenValue, blueValue;
// Normalize LDR value to 0-255 range
int normalizedValue = map(ldrValue, 0, 1023, 0, 255);
if (normalizedValue <= 85) { // Lower third of light levels
// Transition from green to blue
greenValue = 255 - (normalizedValue * 3);
blueValue = normalizedValue * 3;
redValue = 0;
} else if (normalizedValue <= 170) { // Middle third of light levels
// Transition from blue to red
blueValue = 255 - ((normalizedValue - 85) * 3);
redValue = (normalizedValue - 85) * 3;
greenValue = 0;
} else { // Upper third of light levels
// Red is dominant
redValue = 255;
greenValue = blueValue = 0;
}
// Set the RGB LED color
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
Serial.println(normalizedValue);
delay(100); // Short delay for stability
}