Audio-Servo control: Enchanted Tiki Bird

Hi everyone! I am currently building one of the tiki birds from Walt Disney’s Enchanted Tiki Room in Disneyland and am want to control the beak movement with audio. At the moment, I have a servo attached to pin 9, 5V and GND, and an audio jack is hooked up to GND and A0. The audio is played through a 3.5mm cord from my iPhone. I’m using the code below, but it is a little shaky, and whenever there isn’t audio playing the servo slowly starts to spin and will spaz out. I’m kind of stuck at this point, and any help would be greatly appreciated. Thanks and have a magical day!

#include <Servo.h>

Servo myservo;

int ServoPin = 9;
int SoundInPin = A0;

void setup() {

pinMode(SoundInPin, INPUT);

void loop() {
int sensorValue = analogRead(SoundInPin);
sensorValue = map(sensorValue,0,512,0,180);



Two things:

1.) Analog signals from iPhones and other audio devices swing both above and below ground. This means you need to block the negative voltages to protect the Arduino's ADC (signal rectification - use a full wave rectifier).
2.) The servo shouldn't directly respond to the signal's amplitude like you have. You should consider making a lowpass filter (either in hardware or software) to process the output of your rectification circuit before doing servo control.

You also might need to amplify the signal.