code.py
 1  # SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  import time
 4  import random
 5  import board
 6  import audiomp3
 7  import audiopwmio
 8  from adafruit_crickit import crickit
 9  
10  ss = crickit.seesaw  # Crickit seesaw setup
11  
12  button = crickit.SIGNAL1  # momentary switch to trigger animation
13  ss.pin_mode(button, ss.INPUT_PULLUP)
14  
15  LED = crickit.SIGNAL4  # standard LED for eyeball lighting
16  ss.pin_mode(LED, ss.OUTPUT)
17  
18  attract_switch = crickit.SIGNAL8  # attract mode switch or jumper
19  ss.pin_mode(attract_switch, ss.INPUT_PULLUP)
20  
21  audio = audiopwmio.PWMAudioOut(board.A0)  # Feather outputs this pin to Crickit amplifier
22  audio_files = [  # use your own mono .mp3 files
23                 "phrase_01.mp3",
24                 "phrase_02.mp3",
25                 "phrase_03.mp3"
26  ]
27  current_audio_file = 0
28  
29  # two motors
30  motor_eye = crickit.dc_motor_1
31  motor_lid = crickit.dc_motor_2
32  
33  def open_lid():
34      motor_lid.throttle = 1  # full speed open
35      time.sleep(0.25)
36      motor_lid.throttle = 0  # hold
37  
38  def close_lid():
39      motor_lid.throttle = -1  # full speed closed
40      time.sleep(0.25)
41      motor_lid.throttle = 0
42  
43  def blink(times):
44      for _ in range(times):
45          ss.digital_write(LED, True)
46          time.sleep(0.1)
47          ss.digital_write(LED, False)
48          time.sleep(0.1)
49  
50  def eye_look():
51      motor_eye.throttle = random.uniform(0.6, 1.0)
52      time.sleep(random.random())  # 0 to 1.0 seconds
53      motor_eye.throttle = 0
54      time.sleep(random.random())
55      motor_eye.throttle = random.uniform(-1.0, -0.6)
56      time.sleep(random.random())
57      motor_eye.throttle = 0
58      time.sleep(random.random())
59  
60  
61  
62  while True:
63      if ss.digital_read(attract_switch):  #  regular mode, attrack switch not closed/shorted
64          if not ss.digital_read(button):  # button has been pressed
65              decoder = audiomp3.MP3Decoder(open("ring.mp3", "rb"))
66              audio.play(decoder)
67              while audio.playing:
68                  pass
69              open_lid()
70              blink(3)
71              ss.digital_write(LED, True)  # light the eye
72              decoder = audiomp3.MP3Decoder(open(audio_files[current_audio_file], "rb"))
73              audio.play(decoder)
74              while audio.playing:
75                  eye_look()
76              motor_eye.throttle = 0  # audio is finished, pause the eye
77              blink(5)
78              close_lid()
79              current_audio_file = ((current_audio_file + 1) % (len(audio_files)))  # go to next file
80  
81      else:  # attract mode
82          open_lid()
83          blink(3)
84          ss.digital_write(LED, True)
85          for _ in range(4):
86              eye_look()
87          time.sleep(1)
88          blink(5)
89          close_lid()
90          time.sleep(random.randint(2, 8))