Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Werewolf Stocking - Adafruit Circuit Playground Express, neopixels, servo, sound
# CircuitPlaygroundExpress_LightSensor
# reads the on-board light sensor and graphs the brighness with NeoPixels
import time
import board
import neopixel
from analogio import AnalogIn
from simpleio import map_range
import random
import audioio
import digitalio
import adafruit_motor.servo
import pulseio
pwm = pulseio.PWMOut(board.A2, frequency=50)
# from adafruit_motor import servo
servo = adafruit_motor.servo.Servo(pwm)
servoBegin = 45
servoEnd = 180
# enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True
# create a PWMOut object on Pin A2.
# pwm = pulseio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50)
# neopixel brightness ---------------------
xb = .06
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=0, brightness=xb)
pixelsStrip = neopixel.NeoPixel(board.A1, 37, auto_write=0, brightness=xb)
pixels.fill((0, 0, 0))
pixels.show()
pixelsStrip.fill((0, 0, 0))
pixelsStrip.show()
analogin = AnalogIn(board.LIGHT)
audiofiles = ["wolfpy.wav"]
def play_file(filename):
print("Playing file: " + filename)
wave_file = open(filename, "rb")
with audioio.WaveFile(wave_file) as wave:
with audioio.AudioOut(board.A0) as audio:
audio.play(wave)
while audio.playing:
pass
print("Finished")
while True:
# light value remaped to pixel position
peak = map_range(analogin.value, 2000, 62000, 0, 9)
peak2 = map_range(analogin.value, 2000, 62000, 0, 36)
print(analogin.value, int(peak))
if peak > 0:
for p in range(0, 3):
time.sleep(.1)
pixels[1] = (255, 0, 0)
pixels[2] = (255, 0, 0)
pixels[3] = (255, 0, 0)
pixels[6] = (255, 0, 0)
pixels[7] = (255, 0, 0)
pixels[8] = (255, 0, 0)
pixels.show()
time.sleep(0.1)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep(0.1)
pixels[1] = (255, 0, 0)
pixels[2] = (255, 0, 0)
pixels[3] = (255, 0, 0)
pixels[6] = (255, 0, 0)
pixels[7] = (255, 0, 0)
pixels[8] = (255, 0, 0)
pixels.show()
else:
pixels.fill((0, 0, 0))
for k in range(0, 36, 1):
if k < peak2:
pixelsStrip[k] = (255, 0, 0)
else:
pixelsStrip[k] = (0, 0, 0)
pixels.show()
pixelsStrip.show()
if peak > 8:
play_file(audiofiles[0])
for n in range(0, random.randint(0, 4)):
for anglex in range(servoBegin, servoEnd, 5):
servo.angle = anglex
time.sleep(random.random()*0.05)
for anglex in range(servoEnd, servoBegin, -5):
servo.angle = anglex
time.sleep(random.random()*0.05)
time.sleep(0.1)
pixels.fill((0, 0, 0))
pixelsStrip.fill((0, 0, 0))
if peak == 0:
for l in range(0, 10, 1):
m = random.randint(0, 36)
pixelsStrip[m] = (255, 255, 255)
pixelsStrip.show()
time.sleep(random.random()*.2)
pixelsStrip[m] = (0, 0, 0)
pixelsStrip.show()
time.sleep(random.random()*.2)
if random.randint(0, 9) < 4:
for n in range(0, random.randint(0, 4)):
for anglex in range(servoBegin, servoEnd, 5):
servo.angle = anglex
time.sleep(random.random()*0.05)
for anglex in range(servoEnd, servoBegin, -5):
servo.angle = anglex
time.sleep(random.random()*0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.