Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. # a fun sleep assistant for toddlers adapting to life in a "big bed"
  2. # when the circuit playground express lights up, it's OK to get up.
  3. # if the playground is not yet lit up, please let the parent(s) sleep just a
  4. # little longer...
  5. import time
  6.  
  7. import audioio
  8. import board
  9. from digitalio import DigitalInOut, Direction, Pull
  10. import neopixel
  11.  
  12.  
  13. button_a = DigitalInOut(board.BUTTON_A)
  14. button_a.direction = Direction.INPUT
  15. button_a.pull = Pull.DOWN
  16.  
  17. button_b = DigitalInOut(board.BUTTON_B)
  18. button_b.direction = Direction.INPUT
  19. button_b.pull = Pull.DOWN
  20.  
  21. spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
  22. spkrenable.direction = Direction.OUTPUT
  23. spkrenable.value = True
  24.  
  25. speaker = audioio.AudioOut(board.A0)
  26.  
  27. pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.01)
  28. pixels.fill((0, 0, 0))
  29. pixels.show()
  30.  
  31.  
  32. def wheel(pos):
  33. # Input a value 0 to 255 to get a color value.
  34. # The colours are a transition r - g - b - back to r.
  35. if pos < 85:
  36. return (int(pos * 3), int(255 - (pos * 3)), 0)
  37. elif pos < 170:
  38. pos -= 85
  39. return (int(255 - (pos * 3)), 0, int(pos * 3))
  40. else:
  41. pos -= 170
  42. return (0, int(pos * 3), int(255 - pos * 3))
  43.  
  44.  
  45. def rainbow_cycle(wait):
  46. for j in range(255):
  47. for i in range(len(pixels)):
  48. idx = int((i * 256 / len(pixels)) + j * 10)
  49. pixels[i] = wheel(idx & 255)
  50. pixels.show()
  51. time.sleep(wait)
  52.  
  53.  
  54. while True:
  55. # Button A triggers "Prepare the (sleep) environment!"
  56. if button_a.value:
  57. rainbow_cycle(.001)
  58. pixels.fill((0, 0, 0))
  59. pixels.show()
  60. # sleep for 12 hours, then show purple neopixel ring
  61. time.sleep(12 * 60 * 60)
  62. # the purple neopixels means it's OK to get up and start the day!
  63. pixels.fill((255, 0, 255))
  64. pixels.show()
  65. # Button B resets the environment after a sleep cycle has completed
  66. elif button_b.value:
  67. pixels.fill((0, 0, 0))
  68. pixels.show()
  69.  
  70. time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement