from machine import Pin
from neopixel import NeoPixel
from time import *
from pin_numbers import *
print('NeoPixel Example')
print('press the button to animate neopixels')
pixels = NeoPixel(Pin(D0), 2)
btn = Pin(D8, Pin.IN, Pin.PULL_UP)
btn_val_last = 1
led_state = 'red'
pixels[0] = (255, 0, 0)
pixels[1] = (0, 255, 0)
pixels.write()
while True:
#print('btn =', btn.value())
# check that the button changed from high to low:
if btn.value() == 0: # button value is low
if btn_val_last == 1: # last button value was high
# fade from red to green:
if led_state == 'red':
for i in range(1023):
r_led.duty(1023-i)
g_led.duty(i)
sleep_ms(1)
#b_led.on()
led_state = 'green'
# change from green to blue:
elif led_state == 'green':
r_led.off()
g_led.off()
b_led.on()
led_state = 'blue'
# change from blue to red:
elif led_state == 'blue':
r_led.on()
g_led.off()
b_led.off()
led_state = 'red'
print('led_state =', led_state)
# update the last button value:
btn_val_last = btn.value()
sleep_ms(100)Loading
xiao-esp32-c3
xiao-esp32-c3