Audio-Servo Code Help

Hello,

I want to make a talking skull and I want to use a sound sensor to drive a MG996R servo. The reason I am using a sound sensor is because i’m using a MOVI shield which outputs the audio through a speaker, and I want the sound sensor to detect when there is sound and move the jaw accordingly. [Also, i’m using a 3 pin sound sensor ( VCC, GND, OUT)].

Thanks in advance.

#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor=2;
int LED=4;
boolean LEDStatus=false;
//All GNDs get connected together and go to Arduino GND
//All Positives get connected together and get connected to Arduino 5V
//Servo Signal goes to pin 9
//Sound sensor Signal (out) goes to pin 2
void setup() {
myservo.attach(9);
pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);

}

void loop() {

int SensorData=digitalRead(soundSensor);
if(SensorData==1){

if(LEDStatus==false){
LEDStatus=true;
digitalWrite(LED,HIGH);
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
else{
LEDStatus=false;
digitalWrite(LED,LOW);
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}

So, apart from the way in which your code is formatted and posted here, is the problem ?

Did you Read this before posting a programming question

So is it working or is there something you want help with? Or is that a "classified secret"?

Steve

Ok, I guess I forgot to mention the problem. The problem i'm having is that the servo doesn't stop right when the sound stops. Also it takes too long to reach the position. I tried to decrease the delay speed to 5 and my servo rapidly spasms. So, I need it to move as in sync as accuracy as possible and as fast as possible.

Thanks.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. You can edit your first post to include the code tags and indenting.

Is the servo powered by the Arduino or an external (separate) power supply?

servo doesn’t stop right when the sound stops

That is not really a surprise because you have a 90 step for loop running that will run to completion. If you want it to stop and possibly return to a “home” position wherever it is when the sound stops then either don’t use a for loop or read the sound level inside the for loop and break out of it when the sound stops

I need it to move as in sync as accuracy as possible and as fast as possible.

So why use a for loop and a delay() instead of a single write() to send it to the target position ?

Here is your code formatted using Auto format in the IDE and posted here in code tags whuich is the preferred method

#include <Servo.h>
Servo myservo;
int pos = 0;
int soundSensor = 2;
int LED = 4;
boolean LEDStatus = false;
//All GNDs get connected together and go to Arduino GND
//All Positives get connected together and get connected to Arduino 5V
//Servo Signal goes to pin 9
//Sound sensor Signal (out) goes to pin 2
void setup()
{
  myservo.attach(9);
  pinMode(soundSensor, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  int SensorData = digitalRead(soundSensor);
  if (SensorData == 1)
  {
    if (LEDStatus == false)
    {
      LEDStatus = true;
      digitalWrite(LED, HIGH);
      for (pos = 0; pos <= 90; pos += 1)   // goes from 0 degrees to 180 degrees
      {
        // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }
    else
    {
      LEDStatus = false;
      digitalWrite(LED, LOW);
      for (pos = 90; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
      {
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }
  }
}

Ok, Lets say you start from scratch. How would you the the servo to move with the sound in the simplest way possible? I am a beginner at coding. I am doing something like this as well

@classified123 why do you and @classifiedsecret have the same IP address and a very similar username ?

Ah a person who thinks that rules shouldn't apply to him.

O.k. you don't want to play by the site rules then I for one don't want to bother helping you. Good luck.

Steve