from machine import Pin
from neopixel import NeoPixel
from time import *
print('NeoPixel LED + button Example')
# button input on pin 2:
btn = Pin(2, Pin.IN, Pin.PULL_UP)
btn_val_last = 1
# create NeoPixel driver on pin 13 for 16 pixel:
np = NeoPixel(Pin(13), 16)
print('begin with white color for 2 seconds..')
for i in range(16):
np[i] = (255, 255, 255)
# write data to all pixels:
np.write()
# delay 2 seconds:
sleep_ms(2000)
program_state = 'START'
print('start fading green color up and down..')
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
# program state transitions between START and STOP states:
if program_state == 'START':
program_state = 'STOP'
elif program_state == 'STOP':
program_state = 'START'
print('program_state =', program_state)
# update the last button value:
btn_val_last = btn.value()
# program behaviors for START and STOP states:
if program_state == 'STOP':
# set all pixels to red color during STOP state:
for i in range(16):
np[i] = (255, 0, 0)
np.write()
sleep_ms(100)
elif program_state == 'START':
# fade green color up and down during START state:
for g in range(100):
for i in range(16):
np[i] = (0, g, 0)
np.write()
sleep_ms(1)
for g in range(100):
for i in range(16):
np[i] = (0, 100-g, 0)
np.write()
sleep_ms(1)