from machine import Pin, ADC
import time
red_led = Pin(3, Pin.OUT)
green_led = Pin(10, Pin.OUT)
blue_led = Pin(28, Pin.OUT)
ldr = ADC(Pin(27))
def detect_color(light_value):
print(f"Light Value: {light_value}")
if light_value < 500:
# Simulate red color detection (low light intensity)
red_led.on()
green_led.off()
blue_led.off()
print('Detected color: Red')
elif 500 <= light_value < 1500:
# Simulate green color detection (medium light intensity)
red_led.off()
green_led.on()
blue_led.off()
print('Detected color: Green')
elif 1500 <= light_value < 2500:
# Simulate blue color detection (higher light intensity)
red_led.off()
green_led.off()
blue_led.on()
print('Detected color: Blue')
else:
# If the light intensity is very high, we turn off all LEDs (no color detected)
red_led.off()
green_led.off()
blue_led.off()
print('No significant color detected')
# Main loop
while True:
# Read the value from LDR (light sensor)
light_value = ldr.read_u16()
# Normalize light_value to a more appropriate range for comparison (0-4095 range)
light_value = light_value / 1024 * 4095 # Normalize LDR value (0-4095 range)
# Simulate color detection based on the LDR reading
detect_color(light_value)
# Delay for a second before taking the next reading
time.sleep(1)