Update ufo.py

tilt changes color, pitch, frequency
This commit is contained in:
John Edgar Park 2017-10-23 10:40:50 -07:00 committed by GitHub
parent c3a351def5
commit 6abb0ebc9f

View file

@ -1,5 +1,6 @@
# UFO Circuit Playground Express
# Plays UFO lights and sounds if the board is upside down only!
# Plays UFO lights and sounds if the board is upside down only,
# Tilt to change light color, cycle speed, tone pitch
from adafruit_circuitplayground.express import cpx
import neopixel
@ -7,15 +8,15 @@ import board
import time
# The two files assigned to buttons A & B
audiofiles = ["ufo.wav", "ufoUp.wav"]
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=.8)
def simpleCircle(wait, R, G, B): # timing, color values per channel
baseFreq = int(20 + (G * 0.3)) # tone value derived from rotation
for i in range(10):
pixels[i] = ((0, 0, 0))
cpx.start_tone(baseFreq + i) # increasing pitch sweep
time.sleep(wait)
for i in range(10):
@ -24,22 +25,21 @@ def simpleCircle(wait, R, G, B): # timing, color values per channel
# Main loop gets x, y and z axis acceleration, prints the values, and turns on
# lights if the UFO is upside down, plays music
while True:
R = 0
G = 0
B = 0
x, y, z = cpx.acceleration # read the accelerometer values
# print(x, y, z)
R = 10 * (R + abs(int(x))) # scale up the accel values into color values
x, y, z = cpx.acceleration # read the accelerometer values
R = 10 * (R + abs(int(x))) # scale up the accel values into color values
G = 10 * (G + abs(int(y)))
B = 10 * (B + abs(int(z)))
# print(R, G, B)
# check for upside down state on z axis
if z < 0: # any negative number on z axis means it's upside down enough
simpleCircle(.03, R, G, B)
cpx.play_file(audiofiles[0])
if z < 0: # any negative number on z axis means it's upside down enough
speed=(0.01 * (B * 0.025))
simpleCircle(speed, R, G, B) # speed based on tilt, .01 is good start
else: # right side up, no colors or sound!
else: # right side up means no colors or sound!
pixels.fill((0, 0, 0))
cpx.stop_tone()