I need to activate rainbow demo when CPB is shaked and lights turned off when not shaked, but i can't figure how to do it. What shake function needs my code?
Thanks in advance.
Code: Select all
import time
import board
import neopixel
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False)
# choose which demos to play
# 1 means play, 0 means don't!
rainbow_demo = 1
rainbow_cycle_demo = 1
def rainbow_cycle(wait):
for j in range(255):
for i in range(10):
rc_index = (i * 256 // 10) + j * 5
pixels[i] = wheel(rc_index & 255)
pixels.show()
time.sleep(wait)
def rainbow(wait):
for j in range(255):
for i in range(len(pixels)):
idx = int(i + j)
pixels[i] = wheel(idx & 255)
pixels.show()
time.sleep(wait)
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:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
while True:
if rainbow_cycle_demo:
rainbow_cycle(0.05)
if rainbow_demo:
rainbow(0.05)