Setting up the Arduino Compatible Mini Sound Sensor for my Victorian bustle project was a bit of a challenge. This is not a complaint regarding the device. The sensor is simplicity itself; hook vcc to your 5V, gnd to ground, and OUT to one of your Arduino's analog inputs. Interpreting those values (which in theory could be as high as 1024, but I've never actually had peak above 400,) in a meaningful way is more of a challenge.
The goal was to get the lights in the bustle gown to pulse to music. Fans, low level chatter, and other ambient noises are not a part of the music, and as such should be filtered as much as possible. Determining the ambient noise level is, again, simplicity incarnate; just have it collect a steady sum of incoming values and divide that against the number of samples collected to get a running average. Collecting a peak level is just as simple; have a global peak variable (initialized to 0.) If the collected sample is above this peak level, it becomes the new peak level.
So you have your average and your peak values. To determine how brightly your LED's should fire, all you have to do is determine where your collected value falls between these two values, and map that to a number that's meaningful to your RGB (which accepts numbers between 0 and 254.) A function called 'map' does just that. Map takes five arguments: an input value (val below,) the range this value should fall into (two values; valAvg (my average) and globalPeak (my ceiling,) and the range you want to put it into (two values: 0 and 254).
int soundNum=map(val,valAvg,globalPeak,0,254);
So you've got your calibrated sound value. Probably. Problems could occur above if val somehow eluded my range. Let's say a scandal occurs, the room falls silent for >30ms (an eternity in computer time.) The sensor pulls a 0. My average is 5. As this falls outside the expected input range, ti will also be mapped outside the output range. The result could be -1, a number my LED's (expecting a range of 0-254) won't care for.
The solution is constrain. Constrain takes three arguments, your initial value (soundNum below,) a floor (0,) and a ceiling (254.)
soundNum=constrain(soundNum,0,254);
If, in the above example, soundNum was initially -15, it would be set to 0. If it was 1024, it would be set to 254.
Beautiful. We now have something that SHOULD accurately represent a calibrated sound level within the room. We don't HAVE to trust math, of course. With a simple adjustment of a steampunk dial, my lovely wife can have some level of manual control over the interpreted input value. Still, proper calibration makes the whole process easier.
Hello, My name is Peter and I am attempting to pulse an RGB strip to music also. Is there any way you could upload the code that you used for your project? Thanks!
ReplyDelete