Hello, I am trying to create LEDs that react to the various sounds created by a game I have searched google for days now and can not find anything that can help me.
I am using the attached circuit to read data in from a pair of old earbuds with the speaks cut off.
Here is the code I have so far any help would be appreciated
// Arduino Beat Detector By Damian Peckett 2015
// License: Public Domain.
// Our Global Sample Rate, 5000hz
#define SAMPLEPERIODUS 200
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
void setup() {
// Set ADC to 77khz, max for 10bit
sbi(ADCSRA, ADPS2);
cbi(ADCSRA, ADPS1);
cbi(ADCSRA, ADPS0);
Serial.begin(9600);
//The pin with the LED
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
}
// 20 - 200hz Single Pole Bandpass IIR Filter
float bassFilter(float sample) {
static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = (sample) / 1.4f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
return yv[2];
}
// 10hz Single Pole Lowpass IIR Filter
float envelopeFilter(float sample) { //10hz low pass
static float xv[2] = {0, 0}, yv[2] = {0, 0};
xv[0] = xv[1];
xv[1] = sample / 40.f;
yv[0] = yv[1];
yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
return yv[1];
}
// 1.7 - 3.0hz Single Pole Bandpass IIR Filter
float beatFilter(float sample) {
static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = sample / 2.7f;
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
return yv[2];
}
void loop() {
int a = 0;
unsigned long time = micros(); // Used to track rate
float sample, value, envelope, beat, thresh;
unsigned char i;
for (i = 0;; ++i) {
// Read ADC and center so +-512
sample = (float)analogRead(0);
// Serial.println(sample);
// Filter only bass component
value = bassFilter(sample);
// Take signal amplitude and filter
if (value < 0)value = -value;
envelope = envelopeFilter(value);
// Every 200 samples (25hz) filter the envelope
if (i == 200) {
// Filter out repeating bass sounds 100 - 180bpm
beat = beatFilter(envelope);
// Threshold it based on potentiometer on AN1
thresh = 0.01f * (float)analogRead(1);
// If we are above threshold, light up LED
Serial.println(thresh);
if (beat > thresh) {
a = 0;
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(16, HIGH);
digitalWrite(17, HIGH);
digitalWrite(18, HIGH);
digitalWrite(19, HIGH);
}
/*if (beat >.01 ) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}*/
if (beat < .03 >.02) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(16, LOW);
digitalWrite(17, LOW);
digitalWrite(18, LOW);
digitalWrite(19, LOW);
}
else {}
i = 0;
//Reset sample counter
}
// Consume excess clock cycles, to keep at 5000 hz
for (unsigned long up = time + SAMPLEPERIODUS; time > 20 && time < up; time = micros());
}
}