33# SPDX-License-Identifier: MIT
44
55import os
6- import time
76import random
87import board
98import audiocore
1211import pwmio
1312import neopixel
1413import adafruit_lis3dh
15- from random import randint
1614from adafruit_ticks import ticks_ms , ticks_add , ticks_diff
1715from digitalio import DigitalInOut , Direction , Pull
1816from adafruit_motor import servo
3230lis3dh = adafruit_lis3dh .LIS3DH_I2C (i2c , int1 = int1 )
3331lis3dh .range = adafruit_lis3dh .RANGE_2_G
3432
35- # Setup button switch
3633switch = DigitalInOut (board .EXTERNAL_BUTTON )
3734switch .direction = Direction .INPUT
3835switch .pull = Pull .UP
5653mixer .voice [0 ].play (wave )
5754
5855def open_audio (num ):
59- wav_filename = wavs [num ]
60- wav_file = open (wav_filename , "rb" )
61- wave = audiocore .WaveFile (wav_file )
62- return wave
56+ n = wavs [num ]
57+ f = open (n , "rb" )
58+ w = audiocore .WaveFile (f )
59+ return w
6360
6461PIXEL_PIN = board .EXTERNAL_NEOPIXELS
6562SERVO_PIN = board .EXTERNAL_SERVO
6663NUM_PIXELS = 8
6764ORDER = neopixel .GRB
6865BRIGHTNESS = 0.3
6966
70- # Initialize servo
7167PWM = pwmio .PWMOut (SERVO_PIN , duty_cycle = 2 ** 15 , frequency = 50 )
7268SERVO = servo .Servo (PWM )
7369
74- # Initialize NeoPixels and animations
7570pixel = neopixel .NeoPixel (board .NEOPIXEL , 1 )
7671pixel .brightness = 1
7772
7873PIXELS = neopixel .NeoPixel (PIXEL_PIN , NUM_PIXELS , auto_write = False ,
7974 pixel_order = ORDER )
80- LARSON = Comet (PIXELS , bounce = True , speed = 0.07 ,
75+ LARSON = Comet (PIXELS , bounce = True , speed = 0.7 ,
8176 tail_length = NUM_PIXELS // 2 ,
82- color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
83- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
84- BLUE [2 ] * BRIGHTNESS )) # setting.
77+ color = (BLUE [0 ] * BRIGHTNESS ,
78+ BLUE [1 ] * BRIGHTNESS ,
79+ BLUE [2 ] * BRIGHTNESS ))
8580pulse = Pulse (PIXELS , speed = 0.05 ,
86- color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
87- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
88- BLUE [2 ] * BRIGHTNESS ), period = 3 )
81+ color = (BLUE [0 ] * BRIGHTNESS ,
82+ BLUE [1 ] * BRIGHTNESS ,
83+ BLUE [2 ] * BRIGHTNESS ), period = 3 )
8984sparkle = Sparkle (PIXELS , speed = 0.2 ,
90- color = (RED [0 ] * BRIGHTNESS , # This is a little faster than
91- RED [1 ] * BRIGHTNESS , # using the NeoPixel brightness
92- RED [2 ] * BRIGHTNESS ), num_sparkles = 10 )
85+ color = (RED [0 ] * BRIGHTNESS ,
86+ RED [1 ] * BRIGHTNESS ,
87+ RED [2 ] * BRIGHTNESS ), num_sparkles = 10 )
9388
9489SERVO .angle = POSITION = NEXT_POSITION = 90
95- MOVING = False # Initial state = paused
96- START_TIME = ticks_ms () # Initial time
97- DURATION = 1000 # Hold initial position for 1 sec
90+ MOVING = False
91+ START_TIME = ticks_ms ()
92+ DURATION = 1000
9893
9994adabot_talk = False
10095
@@ -106,43 +101,37 @@ def open_audio(num):
106101while mixer .playing :
107102 LARSON .animate ()
108103
109- while True : # Loop forever...
104+ while True :
110105 if ticks_diff (ticks_ms (), clock ) >= prop_time :
111106 x , y , z = [
112107 value / adafruit_lis3dh .STANDARD_GRAVITY for value in lis3dh .acceleration
113108 ]
114109 if z > 0.9 :
115110 adabot_nap = True
116111 SERVO .angle = POSITION = NEXT_POSITION = 90
117- pulse .color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
118- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
119- BLUE [2 ] * BRIGHTNESS )
120112 else :
121113 adabot_nap = False
122- LARSON .color = (BLUE [0 ] * BRIGHTNESS , # This is a little faster than
123- BLUE [1 ] * BRIGHTNESS , # using the NeoPixel brightness
124- BLUE [2 ] * BRIGHTNESS )
125114 if not adabot_nap :
126115 MOVING = not MOVING
127- if MOVING : # Switching from paused to moving
116+ if MOVING :
128117 POSITION = NEXT_POSITION
129- while abs (POSITION - NEXT_POSITION ) < 10 : # Min +/- 10 degrees
130- NEXT_POSITION = random .uniform (0 , 180 ) # Try, try again
118+ while abs (POSITION - NEXT_POSITION ) < 10 :
119+ NEXT_POSITION = random .uniform (0 , 180 )
131120 DURATION = 0.2 + 0.6 * abs (POSITION - NEXT_POSITION ) / 180
132- else : # Switching from moving to paused
133- SERVO .angle = NEXT_POSITION # Move to end of sweep
134- DURATION = random .uniform (0.5 , 2.5 ) # Pause time
121+ else :
122+ SERVO .angle = NEXT_POSITION
123+ DURATION = random .uniform (0.5 , 2.5 )
135124 clock = ticks_add (clock , prop_time )
136125 if MOVING :
137- FRACTION = 0.0 / DURATION # Linear 0 to 1
138- FRACTION = (3 * FRACTION ** 2 ) - (2 * FRACTION ** 3 ) # Ease in/out
126+ FRACTION = 0.0 / DURATION
127+ FRACTION = (3 * FRACTION ** 2 ) - (2 * FRACTION ** 3 )
139128 SERVO .angle = POSITION + (NEXT_POSITION - POSITION ) * FRACTION
140129 if adabot_talk :
141- wave = open_audio (randint (1 , 17 ))
130+ wave = open_audio (random . randint (1 , 7 ))
142131 mixer .voice [0 ].play (wave )
143132 while mixer .playing :
144133 sparkle .animate ()
145- else :
134+ if not mixer . playing :
146135 adabot_talk = False
147136 PIXELS .fill (BLACK )
148137 PIXELS .show ()
0 commit comments