Changes by Limor

Code updated by Limor to respond to light/dark more quickly
This commit is contained in:
Dano Wall 2019-01-17 11:57:01 -05:00 committed by GitHub
parent 10e2ebcc37
commit 8000acefb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,53 +1,33 @@
# Music Box code in CircuitPython - Dano Wall and Mike Barela
# Revised by Ladyada 2019-01-16
import os
import random
from busio import I2C
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo
from adafruit_crickit import crickit
from analogio import AnalogIn
from simpleio import map_range
import neopixel
import audioio
import board
import time
# Create seesaw object
i2c = I2C(board.SCL, board.SDA)
seesaw = Seesaw(i2c)
AUDIO_FILENAME = 'fur-elise.wav'
# Create one servo on seesaw servo 2
pwm = PWMOut(seesaw, 16)
pwm.frequency = 50
myservo = servo.Servo(pwm)
myservo.angle = 0
# Find all Wave files on the storage
wavefiles = [file for file in os.listdir("/")
if (file.endswith(".wav") and not file.startswith("._"))]
if len(wavefiles) < 1:
print("No wav files found in root directory")
else:
print("Audio files found: ", wavefiles)
# audio output
# Audio output
cpx_audio = audioio.AudioOut(board.A0)
audio = audioio.WaveFile(open(AUDIO_FILENAME, "rb"))
def play_file(filename):
f = open(filename, "rb")
wav = audioio.WaveFile(f)
cpx_audio.play(wav)
# Rotating dancer
dancer = crickit.servo_2
dancer.angle = 0
MAX_SERVO_ANGLE = 160
move_direction = 1
# neopixels!
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
pixels.fill((0, 0, 0))
# light sensor
rawlight = AnalogIn(board.LIGHT)
def light():
peak = map_range(rawlight.value, 2000, 62000, 0, 1023) # map to 0 to 1023
return int(peak)
light = AnalogIn(board.LIGHT)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
@ -66,28 +46,28 @@ def rainbow(value):
for i in range(10):
pixels[i] = wheel((value * i) & 255)
index = 0
sign = 1
while True:
if light() < 130:
# Turn things off if light level < value
# turn off LEDs so we can tell if its dark out!
pixels.brightness = 0
# read light level
light_level = light.value
# turn LEDs back on
pixels.brightness = 1
# Turn things off if light level < value, its dark
if light_level < 2000:
pixels.fill((0, 0, 0))
# myservo.angle = 0.0
cpx_audio.stop()
else:
if not cpx_audio.playing:
# Start playing the song again
cpx_audio.play(audio)
# calculate servo rotation
if index > 246:
index = 0
sign = sign * -1
# Move servo one slot depending on current direction
if sign == 1:
myservo.angle = int(index / 2)
else:
myservo.angle = 123 - int(index / 2)
# play wav file when index is a multiple of 40 (~6x per
# servo rotation and the sound is not already playing
if (index % 40) == 0 and not cpx_audio.playing:
play_file(random.choice(wavefiles))
rainbow(index)
index += 1
if dancer.angle <= 0:
move_direction = 1
if dancer.angle > MAX_SERVO_ANGLE:
move_direction = -1
# Move servo one degree forward or backward.
rainbow(int(dancer.angle * 255/MAX_SERVO_ANGLE))
dancer.angle += move_direction