Created
July 10, 2019 00:41
-
-
Save corbinbs/daf90320dc2202e7e747c3c4d15e83cf to your computer and use it in GitHub Desktop.
Circuit Playground Express Sleep Assistant for Toddlers ( https://www.adafruit.com/product/3333 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment