Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CPX Werewolf Pack - animatronic tail and neopixel eyes
import time
import pulseio
import board
import neopixel
import random
import adafruit_motor.servo
import audioio
from digitalio import DigitalInOut, Direction, Pull
# enable the speaker
spkrenable = DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = Direction.OUTPUT
spkrenable.value = True
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.04)
pixelsx = neopixel.NeoPixel(board.A2, 8, brightness=.04)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
pixelsx.fill(BLACK)
pixelsx.show()
# pixels.fill(BLACK)
# pixels.show()
# Initialize PWM output for the servo (on pin A2) and servo:
pwm = pulseio.PWMOut(board.A1, frequency=50)
servo = adafruit_motor.servo.Servo(pwm)
# wav
audiofiles = ["wolf8x.wav"]
def play_file(filename):
# print("playing file " + filename)
f = open(filename, "rb")
a = audioio.AudioOut(board.A0, f)
a.play()
while a.playing:
pass
# print("finished")
while True:
for i in range(random.randint(1, 4)):
pixelsx.fill(RED)
# pixels.fill(RED)
pixelsx.show()
# pixels.show()
time.sleep(.2)
pixelsx.fill(BLACK)
# pixels.fill(BLACK)
pixelsx.show()
# pixels.show()
for i in range(random.randint(1, 2)):
play_file(audiofiles[0])
time.sleep(.6*random.random())
# leave eyes glowing...
for i in range(random.randint(0, 1)):
pixelsx.fill(RED)
# pixels.fill(RED)
pixelsx.show()
# pixels.show()
# Center tail vertically down
servo.angle = 90
# Wide tail wag
for i in range(random.randint(0, 4)):
servo.angle = 0
time.sleep(.5)
servo.angle = 180
time.sleep(.7*random.random())
# recenter tail
servo.angle = 90
time.sleep(2*random.random())
# Narrow tail wag
for i in range(random.randint(0, 4)):
servo.angle = 45
time.sleep(.5)
servo.angle = 135
time.sleep(.7*random.random())
servo.angle = 90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment