Skip to content

Instantly share code, notes, and snippets.

@kwalseth
Last active March 30, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwalseth/2fcbd70eee546972ba84e71b99f4e1eb to your computer and use it in GitHub Desktop.
Save kwalseth/2fcbd70eee546972ba84e71b99f4e1eb to your computer and use it in GitHub Desktop.
emergency sounds/lights
import random
import time
import board
import neopixel
import touchio
import digitalio
from audioio import WaveFile
from audioio import AudioOut
touch_pad = board.A3
touch = touchio.TouchIn(touch_pad)
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True
audiofiles = ["rimshot.wav", "siren.wav", ""]
def fireeffect():
pixel1[random.randrange(45)] = (255, 0, 0)
pixel1[random.randrange(45)] = color2
pixel1[random.randrange(45)] = color2
pixel1[random.randrange(45)] = color2
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.8)
pixel1 = neopixel.NeoPixel(board.A1, 45, brightness=0.8)
color2 = (50, 0, 0)
fire = 0
def play_file(filename):
wave_file = open(filename, "rb")
with WaveFile(wave_file) as wave:
with AudioOut(board.SPEAKER) as audio:
audio.play(wave)
while audio.playing:
pass
audio = AudioOut(board.SPEAKER)
wave = WaveFile(open(audiofiles[1], "rb"))
time_started = 0
while True:
now = time.monotonic()
if touch.value:
if not audio.playing:
audio.play(wave, loop=True)
fireeffect()
time_started = now
if now - time_started < 5:
fireeffect()
elif audio.playing:
audio.stop()
pixel1.fill((0, 0, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment