import time
from machine import Pin
# ================== CONFIG ==================
# Button pins (active-low, using internal pull-ups)
BTN_RED = 2 # charging
BTN_GREEN = 3 # ready
BTN_BLUE = 4 # connected
# RGB LED pins (your mapping)
PIN_B = 10
PIN_G = 11
PIN_R = 12
# Set to True if your RGB LED is COMMON ANODE (shared +V)
# Set to False if it's COMMON CATHODE (shared GND)
COMMON_ANODE = False # change this if needed
# Debounce time (seconds)
DEBOUNCE = 0.03
# ============================================
# --- Inputs ---
btn_red = Pin(BTN_RED, Pin.IN, Pin.PULL_UP)
btn_green = Pin(BTN_GREEN, Pin.IN, Pin.PULL_UP)
btn_blue = Pin(BTN_BLUE, Pin.IN, Pin.PULL_UP)
# --- Outputs ---
led_r = Pin(PIN_R, Pin.OUT)
led_g = Pin(PIN_G, Pin.OUT)
led_b = Pin(PIN_B, Pin.OUT)
def drive(pin, on: bool):
"""Drive LED channel depending on common anode/cathode."""
pin.value(0 if (on ^ COMMON_ANODE) else 1)
def set_color(r_on, g_on, b_on):
drive(led_r, r_on)
drive(led_g, g_on)
drive(led_b, b_on)
def all_off():
set_color(False, False, False)
print("Touch a control: RED=charging, GREEN=ready, BLUE=connected")
# Start OFF
all_off()
while True:
r = (btn_red.value() == 0) # active-low
g = (btn_green.value() == 0)
b = (btn_blue.value() == 0)
if r:
set_color(True, False, False) # red
time.sleep(DEBOUNCE)
elif g:
set_color(False, True, False) # green
time.sleep(DEBOUNCE)
elif b:
set_color(False, False, True) # blue
time.sleep(DEBOUNCE)
else:
all_off()
time.sleep(DEBOUNCE)