Skip to content

Instantly share code, notes, and snippets.

@deckerego deckerego/code.py
Created Nov 4, 2019

Embed
What would you like to do?
Create a campfire effect with a Circuit Playground Express. Copy a crackle.wav audio file for the sound effect, and toggle the switch to mute/unmute.
from time import sleep
from random import random
from board import SPEAKER, SLIDE_SWITCH, SPEAKER_ENABLE, NEOPIXEL
from neopixel import NeoPixel
from digitalio import DigitalInOut, Direction, Pull
from audioio import WaveFile
from audioio import AudioOut
from simpleio import map_range
# NeoPixels
pixels = NeoPixel(NEOPIXEL, 10, brightness=0.1, auto_write=False)
# Audio Speaker
speaker_enable = DigitalInOut(SPEAKER_ENABLE)
speaker_enable.direction = Direction.OUTPUT
speaker_enable.value = True
audio_clip = WaveFile(open("crackle.wav", "rb"))
speaker = AudioOut(SPEAKER)
# Toggle Switch
switch = DigitalInOut(SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
def audio_play():
if not speaker.playing:
speaker.play(audio_clip, loop=True)
def audio_stop():
if speaker.playing:
speaker.stop()
def animate_flame():
for id in range(10):
green = map_range(random(), 0.0, 1.0, 0, 96)
pixels[id] = (255, int(green), 0)
pixels.show()
while True:
audio_play() if switch.value else audio_stop()
animate_flame()
sleep(0.1)
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.