How best to move a servo to music?

Hi All! I have a quick question, I want to move a servo in time with music and was wondering how best to do that with an arduino. I found someone on Instructables who made a talking skeleton open its mouth when the volume of the music rose above a certain point but just remapping the analog in signal from my wave shield is not moving the servo as smoothly as I would like, it just sort of jitters around. I guess this makes sense as the voltage changes quite a bit to create an audio wave form. I have a wire from R7 on my wave shield plugged into an analog port. Would the MSGEQ7 chip be the best way to accomplish my goal or is there another way? If anyone is curious I am attempting to move the arms of a portal turret in time to the turret opera. Thanks for your time,

Write a piece of code like this:

Result = Previous result x K + current result x (1-k)
Previous result= Result.

Its called an "exponential forgetter" - and if you set k < 1 the thing smooths an input signal very well - you'll have to play with k to get decent results.

frollard5 years ago
a servo's angle is determined by a pwm pulse -- since you're already using an arduino, you basically need to input the audio, figure out its volume, and convert that loudness to a suitable pwm value. Just feeding the audio to the servo aka, 'random data' as far as pwm is concerned will confuse or possibly hurt it.
Modarius (author)  frollard5 years ago
Yeah, what my sketch right now is doing is sampling the analog in pin and then it remaps the 0- 1026 reading into a 0- 180 value which is then fed into servo.write().
good, now just do a little smoothing, again, the analog value will rise and fall (positive AND negative) thousands of times per second, its not a great value to work with.

Look up code for an arduino Sound Organ or Visual Equalizer - people have figured out good ways to do this already.

Once you have an averaged value (say 1/10th of a second) you can feed THAT value to the servo.