code.py
 1  # SPDX-FileCopyrightText: 2018 John Park for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import board
 7  import neopixel
 8  import audioio
 9  import audiocore
10  import adafruit_crickit
11  
12  print("Adabot Tightrope Unicyclist!")
13  RED =   (16, 0, 0)
14  GREEN = (0, 16, 0)
15  BLACK = (0, 0, 0)
16  
17  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness = 0.2)
18  pixels.fill(BLACK)
19  
20  # Create a motor on Crickit Motor 1 port
21  motor = adafruit_crickit.crickit.dc_motor_1
22  
23  ############### User variables
24  run_time = 6
25  speed = 0.65
26  
27  ############### Music
28  cpx_audio = audioio.AudioOut(board.A0)  # speaker out on Crickit
29  def play_file(wavfile):
30      with open(wavfile, "rb") as f:
31          wav = audiocore.WaveFile(f)
32          cpx_audio.play(wav)
33          while cpx_audio.playing:
34              pass
35  
36  wav_file_name = "circus.wav"
37  play_file(wav_file_name)
38  
39  while True:
40      # set NeoPixels green in direction of movement
41      for i in range(5):
42          pixels[i] = GREEN
43      for i in range(5):
44          pixels[i+5] = BLACK
45  
46      motor.throttle = speed  # full speed forward
47      time.sleep(run_time)  # motor will run for this amount of time
48  
49      # set NeoPixels red when stopped
50      pixels.fill(RED)
51      motor.throttle = 0  # stop the motor
52  
53      # set NeoPixels green in direction of movement
54      for i in range(5):
55          pixels[i] = BLACK
56      for i in range(5):
57          pixels[i+5] = GREEN
58  
59      motor.throttle = -1 * speed  # full speed backward
60      time.sleep(run_time)  # motor will run for this amount of time
61  
62      pixels.fill(RED)
63      motor.throttle = 0  # stopped