Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Electroluminescent beat to the music
// Beat to the music
// Instructables Build Night with Cool Neon
const int p[] = {13, 12, 11, 10, 9}; // List of pins with connected EL wires
const int pn = 5; // number of connected wires
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
delay(200);
for( int i = 0 ; i < pn ; i++ ) { // declare all the Cool Neon channels as outputs
pinMode(p[i], OUTPUT);
digitalWrite(p[i], HIGH);
}
}
void loop() {
byte lights;
byte mask;
while (Serial.available() > 0) {
lights = Serial.read();
Serial.print(lights); // Debug
mask = 00000001;
for (int i = 0; i < pn; i++) { //iterate through bit mask
if (lights & mask){ // if bitwise AND resolves to true
digitalWrite(p[i],LOW); // Turn on
}
else{ //if bitwise and resolves to false
digitalWrite(p[i],HIGH); // Turn off
}
mask <<= 1;
}
}
}
"""
Neon beats, Python control
Intructables Build Night with Cool Neon
"""
import pyaudio
import wave
import sys
import numpy as np
import pylab as pl
import scipy as sp
import scipy.fftpack as fftpack
import serial
CHUNK = 1024 * 2
numchan = 5 # number of lights/channels
sec = CHUNK / numchan;
THRESHOLD = 3.5e6 # FFT threshold, adjust
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
# open stream (2)
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
fs = wf.getframerate()
bytes_per_sample = wf.getsampwidth()
bits_per_sample = bytes_per_sample * 8
dtype = 'int{0}'.format(bits_per_sample)
channels = wf.getnchannels()
ch_left = 0
ch_right = 1
ch = ch_right
com = serial.Serial('/dev/ttyACM0', 115200)
def dB(a,base=1.0):
return 10.0*np.log10(a/base)
def analyze(data):
audio = np.fromstring(data, dtype=dtype)
audio.shape = (audio.shape[0]/channels, channels)
FFT = abs(sp.fft(audio))
freqs = fftpack.fftfreq(audio.size, 1.0/fs)
vals = 0
for i in range(numchan):
vals += 2**i if (sum(FFT[(i*sec):(i+1)*sec])[0] > THRESHOLD) else 0
print vals
com.write(chr(vals))
# read data
data = wf.readframes(CHUNK)
# play stream (3)
while data != '':
analyze(data)
stream.write(data)
data = wf.readframes(CHUNK)
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment