Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # a fun sleep assistant for toddlers adapting to life in a "big bed"
- # when the circuit playground express lights up, it's OK to get up.
- # if the playground is not yet lit up, please let the parent(s) sleep just a
- # little longer...
- import time
- import audioio
- import board
- from digitalio import DigitalInOut, Direction, Pull
- import neopixel
- button_a = DigitalInOut(board.BUTTON_A)
- button_a.direction = Direction.INPUT
- button_a.pull = Pull.DOWN
- button_b = DigitalInOut(board.BUTTON_B)
- button_b.direction = Direction.INPUT
- button_b.pull = Pull.DOWN
- spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
- spkrenable.direction = Direction.OUTPUT
- spkrenable.value = True
- speaker = audioio.AudioOut(board.A0)
- pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.01)
- pixels.fill((0, 0, 0))
- pixels.show()
- 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 < 85:
- return (int(pos * 3), int(255 - (pos * 3)), 0)
- elif pos < 170:
- pos -= 85
- return (int(255 - (pos * 3)), 0, int(pos * 3))
- else:
- pos -= 170
- return (0, int(pos * 3), int(255 - pos * 3))
- def rainbow_cycle(wait):
- for j in range(255):
- for i in range(len(pixels)):
- idx = int((i * 256 / len(pixels)) + j * 10)
- pixels[i] = wheel(idx & 255)
- pixels.show()
- time.sleep(wait)
- while True:
- # Button A triggers "Prepare the (sleep) environment!"
- if button_a.value:
- rainbow_cycle(.001)
- pixels.fill((0, 0, 0))
- pixels.show()
- # sleep for 12 hours, then show purple neopixel ring
- time.sleep(12 * 60 * 60)
- # the purple neopixels means it's OK to get up and start the day!
- pixels.fill((255, 0, 255))
- pixels.show()
- # Button B resets the environment after a sleep cycle has completed
- elif button_b.value:
- pixels.fill((0, 0, 0))
- pixels.show()
- time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement