I'm pretty deep into a project using an Arduino to analyze an ambient audio source and translate it into a beat responsive laser.
Currently I have a micro servo moving the mirror for the laser, and a small microphone with preamp from SparkFun providing the audio input.
The setup looks like so:
I have a video of how it responds right now.
WARNING: The audio is VERY loud in this clip, be sure to turn down your speakers or something so I don't make your ears bleed. Sorry!
This is the code I am using:
#include <Servo.h>
int val = 0;
int envelope = 0;
int envScale = 1;
int toggle = 0;
int sendCount = 0;
Servo servo;
void setup() {
servo.attach(3);
servo.write(25);
}
void loop() {
val = analogRead(A0) - 300;
val = abs(val);
if (val > envelope) {
envelope = val;
envScale = 1;
}
else {
envelope = envelope - envScale;
envScale++;
}
if (envelope > 280 && sendCount > 35 && toggle > 0) {
servo.write(random(35, 47));
sendCount = 0;
toggle = 0;
}
if (envelope > 280 && sendCount > 35 && toggle < 1) {
servo.write(random(23, 35));
sendCount = 0;
toggle = 1;
}
sendCount++;
delay(10);
}
It does not work as well as I had hoped.
I believe it is one of two things, or maybe both.
First possibility:
The microphone sucks. I am using this microphone: SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics
I didn't realize this before purchasing it, but it seems the microphone is only responsive down to 100hz and I believe the low pass I put on the input is about 120hz. Probably works, but could be better for hearing a kick drum.
Additionally, it doesn't have much headroom, which I am not sure how to fix. If the music gets to loud, the input clips, which sucks for a party or something, so I definitely need to replace it with something that has more headroom, and is perhaps more accurate.
Second possibility:
The code sucks.
I know a lot more about music than I do about coding (right now anyways). I know that it at minimum needs to follow the envelope of the signal, because the microphone I have doesn't measure the SPL, it just gives the raw waveform.
Any ideas?
Thanks in advance!
- Justin