Last active
December 1, 2018 17:27
-
-
Save caitlinsdad/f8bfb48163b64e89aeb6cf35622e6b69 to your computer and use it in GitHub Desktop.
Circuit Playground Express Dragon Sign, cap touch and neopixel fire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import neopixel | |
import touchio | |
import audioio | |
from digitalio import DigitalInOut, Direction | |
import random | |
heat = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
cooldown = 0 | |
NUM_LEDS = 10 | |
touch2 = touchio.TouchIn(board.A2) | |
touch3 = touchio.TouchIn(board.A3) | |
touch4 = touchio.TouchIn(board.A4) | |
touch5 = touchio.TouchIn(board.A5) | |
touch6 = touchio.TouchIn(board.A6) | |
touch7 = touchio.TouchIn(board.A7) | |
# enable the speaker | |
spkrenable = DigitalInOut(board.SPEAKER_ENABLE) | |
spkrenable.direction = Direction.OUTPUT | |
spkrenable.value = True | |
# The wav files | |
audiofiles = ["CatEvil.wav", "drum_cowbell.wav"] | |
a = audioio.AudioOut(board.A0) | |
bling = .1 | |
pixelpin = board.A1 | |
pixels = neopixel.NeoPixel(pixelpin, 10, brightness=bling) | |
pixelpinX = board.NEOPIXEL | |
pixelsX = neopixel.NeoPixel(pixelpinX, 10, brightness=bling) | |
pixels.fill((0, 0, 255)) | |
pixels.show() | |
pixelsX.fill((0, 0, 255)) | |
pixelsX.show() | |
time.sleep(0.5) | |
pixels.fill((0, 0, 0)) | |
pixels.show() | |
pixelsX.fill((0, 0, 0)) | |
pixelsX.show() | |
time.sleep(0.5) | |
def play_file(filename): | |
print("playing file " + filename) | |
f = open(filename, "rb") | |
wave = audioio.WaveFile(f) | |
a.play(wave) | |
# Fire code adapted from arduino Kriegsman Fire2012 (tweaking4all.com) | |
def setPixelHeatColor(Pixel, temperature): | |
# Scale 'heat' down from 0-255 to 0-191 | |
t192 = round((temperature/255.0)*191) | |
# calculate ramp up from | |
heatramp = t192 & 0x3F # 0..63 | |
heatramp <<= 2 # scale up to 0..252 | |
# figure out which third of the spectrum we're in: | |
if t192 > 0x80: # hottest | |
pixels[Pixel] = (255, 255, heatramp) | |
else: | |
if t192 > 0x40: # middle | |
pixels[Pixel] = (255, heatramp, 0) | |
else: # coolest | |
pixels[Pixel] = (heatramp, 0, 0) | |
def Fire(Cooling, Sparking): | |
# Step 1. Cool down every cell a little | |
for i in range(0, NUM_LEDS, 1): | |
cooldown = random.randint(0, NUM_LEDS * 2) | |
if cooldown > heat[i]: | |
heat[i] = 0 | |
else: | |
heat[i] = heat[i]-cooldown | |
# Step 2. Heat from each cell drifts 'up' and diffuses a little | |
for k in range(NUM_LEDS - 1, 2, -1): | |
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3 | |
# Step 3. Randomly ignite new 'sparks' near the bottom | |
if random.randint(0, 255) < Sparking: | |
y = random.randint(0, 9) | |
# heat[y] = heat[y] + random.randint(160, 255) | |
heat[y] = random.randint(160, 255) | |
# Step 4. Convert heat to LED colors | |
for j in range(0, NUM_LEDS, 1): | |
setPixelHeatColor(j, heat[j]) | |
pixels.show() | |
for l in range(0,9): | |
heat[l] = 0 | |
while True: | |
pixels[0] = (50, 50, 50) | |
pixelsX.fill((50, 50, 50)) | |
pixels.show() | |
pixelsX.show() | |
if touch2.value: | |
print("Touched TAIL") | |
pixels[0] = (200, 0, 255) | |
time.sleep(0.1) | |
if touch4.value: | |
print("Touched FRONTCLAW") | |
play_file(audiofiles[0]) | |
pixels[0] = (200, 69, 0) | |
time.sleep(0.1) | |
if touch3.value: | |
print("Touched BACKCLAW") | |
play_file(audiofiles[0]) | |
pixels[0] = (255, 160, 0) | |
time.sleep(0.1) | |
if touch5.value: | |
print("Touched WING") | |
pixels[0] = (255, 0, 0) | |
play_file(audiofiles[0]) | |
pixelsX.fill((255, 0, 0)) | |
pixels.fill((0, 0, 0)) | |
Fire(random.randint(50, 70), random.randint(120, 130)) # Fire | |
Fire(random.randint(50, 70), random.randint(120, 130)) # Fire | |
Fire(random.randint(50, 70), random.randint(130, 140)) # Fire | |
time.sleep(0.1) | |
pixels.fill((0, 0, 0)) | |
pixelsX.fill((0, 0, 0)) | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment