LAB : Control a servo with a sensor

I have a lot of micro controllers, sensors and many electrical parts in my late husband room which I do not know what is it, how it works, or how to use it and would like to experiment with it a little. Luckily, I finally found out one sensor, which is a sound sensor that I would like to try to use as a input sensor to activate a servo for my lab.

IMG_7123.JPG

I try to use both analogRead and digitalRead with this sensor but it always seems like it always read and INPUT as 1 all the time even though there is sound or no sound.

Here is my code for the zx-sound sensor to control a servo :

#include <Servo.h>

Servo myservo;

int soundSensor = A5;

int LED = 2;

int pos = 0;

boolean servoState = 0;

void setup() {

 pinMode(soundSensor,INPUT);

 pinMode(LED,OUTPUT);

myservo.attach(9);

}


void loop() {

  int servoData = digitalRead(soundSensor);

  Serial.print("servoData = ");

  Serial.println(servoData);

  if( servoData == 1 ){


    if(servoState == true){

    digitalWrite( 2 , LOW );

    for (pos = 0; pos <= 180; pos += 1) { 

    myservo.write(pos);              

    delay(15);

    }   

    }

    else{

     digitalWrite( 2 , HIGH );

    for (pos = 180; pos >= 0; pos -= 1) {

    myservo.write(pos);            

    delay(15);  }

    }

  }

 } 


I played with it by myself for a couple days and ask my itp-buddy about it but she mentioned that I should better book the office hour and bring all my random sensors to ask for more information.

Therefore, I did another lab to control a servo with touch sensor instead, which is work just find this time with pretty much the same code lines.