from machine import I2C,Pin,ADC
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from time import sleep
addr= 0x27
r=2
c=16
red_pin = Pin(15, Pin.OUT)
green_pin = Pin(12, Pin.OUT)
blue_pin = Pin(13, Pin.OUT)
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
sda_pin=Pin(21)
scl_pin=Pin(22)
i2c=I2C(0,sda=sda_pin,scl=scl_pin,freq=10000)
lcd=I2cLcd(i2c,addr,r,c)
# Function to display values on the LCD
def display_on_lcd(color_name, adc_value):
lcd.clear()
lcd.putstr(f"Color: {color_name}\nADC: {adc_value}")
def display_on_lcd(color_name, adc_value):
lcd.clear()
lcd.putstr(f"Color: {color_name}\nADC: {adc_value}")
# Function to set the RGB LED color
def set_rgb_color(r, g, b):
red_pin.value(r)
green_pin.value(g)
blue_pin.value(b)
# Main loop
while True:
# Read the ADC value from the potentiometer
adc_value = pot.read()
# Map the ADC value to 7 different colors based on the ADC range
if adc_value < 585:
set_rgb_color(0, 1, 1) # Red
color = "Red"
elif adc_value < 1170:
set_rgb_color(1, 0, 1) # Green
color = "Green"
elif adc_value < 1755:
set_rgb_color(1, 1, 0) # Blue
color = "Blue"
elif adc_value < 2340:
set_rgb_color(0, 0, 1) # Yellow (Red + Green)
color = "Yellow"
elif adc_value < 2925:
set_rgb_color(1, 0, 0) # Cyan (Green + Blue)
color = "Cyan"
elif adc_value < 3510:
set_rgb_color(0, 1, 0) # Magenta (Red + Blue)
color = "Magenta"
else:
set_rgb_color(0, 0, 0) # White (Red + Green + Blue)
color = "White"
# Display color and ADC value on the LCD
display_on_lcd(color, adc_value)
# Sleep for a short period (e.g., 0.1 seconds)
sleep(0.1)