import time
from digitalio import DigitalInOut, Direction, Pull
import board
import neopixel
modus = 0
btn = DigitalInOut(board.GP22)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
prev_state = btn.value
pixel_pin = board.GP15
num_pixels = 7
ORDER = neopixel.GRB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1, auto_write=False, pixel_order=ORDER)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
while True:
cur_state = btn.value
if cur_state != prev_state:
if not cur_state:
print("BTN is down")
modus +=1
print(modus)
prev_state = cur_state
if modus==1:
pixels.fill((255,0,0))
pixels.show()
if modus==2:
pixels.fill((0,255,0))
pixels.show()
if modus==3:
pixels.fill((0,0,255))
pixels.show()
if modus==4:
rainbow_cycle(0.001)
if modus==5:
modus =1
prev_state = cur_state