Arduino code for sound sensor moving servo arm

I am using the code below to control a servo motor arm with a sound sensor. The code has some issues I am not seasoned enough to solve. I have played around with the delay numbers trying to get the servo arm to not be so spastic. That is, I want the servo arm to move with the sound emitted without the ‘extra’ movements that don’t match up with the sound emitted.
I appreciate your time.

#include <Servo.h>
Servo arm1;

int degree;
int sensorValue;

void setup() {




void loop() {
  sensorValue = analogRead(A0);

  degree = sensorValue / 100, 500;

  if (sensorValue < 400) {


  else if (sensorValue > 400) {



edit: code tags added by mods, please remember to use them

Welcome to the forums!

I have a few questions to allow me to answer better.

What is the intended result? What are the results? Any Error codes? Do you have the docs for the sound sensor your using?

Feel free to include anything else that might help!


First of all, do you have a 3-pin or a 4-pin sensor?
Could you send a picture of your wiring setup?

Depending on the type of the sensor there are different ways to set it up and send TRIG signals before reading the return value from the sensor each time.

Then, once you get it setup correctly, you should filter out any values outside of the valid range (by simply ignoring them) and only command arm movement after sensing certain in-range value more than once or by doing some sort of smoothing.