import machine as mc
import utime
#Define global variables
# Define the GPIO pins connected to the RGB LED
RED_PIN = mc.Pin(15, mc.Pin.OUT)
GREEN_PIN = mc.Pin(14, mc.Pin.OUT)
BLUE_PIN = mc.Pin(13, mc.Pin.OUT)
#Define GPIO in button
push_bt = mc.Pin(16, mc.Pin.IN, mc.Pin.PULL_UP)
#base values
button_pressed = False
#check whether button pressed
def wait_pin_change(pin):
# wait for pin to change value
# it needs to be stable for a continuous 20ms
cur_value = pin.value()
active = 0
while active < 5:
if pin.value() != cur_value:
active += 1
else:
active = 0
sleep_ms(1)
# Interrupt Service Routine for Button Pressed Events - with no debounce
def button_pressed(change):
global button_pressed
button_pressed = True
# here is how we associate the falling value on the input pin with the callback function
push_bt.irq(handler=button_pressed, trigger=mc.Pin.IRQ_FALLING)
# Function to set the color of the RGB LED
def set_color(red, green, blue):
RED_PIN.value(red)
GREEN_PIN.value(green)
BLUE_PIN.value(blue)
# Main loop to cycle through colors
while True:
if (button_pressed == True):
print("button_pressed")
wait_pin_change(push_bt)
button_pressed = False
if (led.value() == 1):
set_color(255, 0, 0) # Red
utime.sleep(1)
set_color(0, 255, 0) # Green
utime.sleep(1)
set_color(0, 0, 255) # Blue
utime.sleep(1)
print("Done changing")
else:
set_color(0, 0, 0)
print("Im off")
sleep(1)
print("working")
sleep(0.2)