Servo motor with sound sensor

intedeco

New member
I am running the following code to run the motor activated by sound. Need help with code to run the motor for 1 minute before stopping when the "digitalValue==LOW". Thanks

// Include the library
#include <Servo.h>

// Create the servo object
#define servoPin 9

Servo myservo;

// Create a variable to store the servo position:
int angle = 0;


int sensorDigitalPin = 3; // Select the Arduino input pin to accept the Sound Sensor's digital output
int digitalValue; // Define variable to store the digital value coming from the Sound Sensor
int Led13 = 13; // Define LED port; this is the LED built in to the Arduino (labled L)

void setup()
{
pinMode(sensorDigitalPin, INPUT); // Define pin 3 as an input port, to accept digital input
pinMode(Led13, OUTPUT); // Define LED13 as an output port, to indicate digital trigger reached
myservo.attach(servoPin); // attach the servo to our servo object
myservo.write(0);
}

void loop() {

digitalValue = digitalRead(sensorDigitalPin); // Read the value of the digital interface 3 assigned to digitalValue

if (digitalValue == HIGH) // When the Sound Sensor sends signal, via voltage present, light LED13 (L)

{
// scan from 0 to 180 degrees
for (angle = 0; angle < 180; angle++)
{
myservo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for (angle = 180; angle > 0; angle--)
{
myservo.write(angle);
delay(15);
}
}

else
if(digitalValue==LOW) // When there is no sound and LED is OFF)

{
myservo.write(0); // stop the motor
}


}
 
What is the servo doing that is wrong? I see you are going from 0 to 180 (180 steps x 0.015 = 2.7 seconds, assuming the servo can keep up with that)

If your servo is not working or running too fast, then try delay(333) // 180 * 0.333 = 60 seconds

Also, are you hoping the servo will run continuously? That is a different servo type.
 
What is the servo doing that is wrong? I see you are going from 0 to 180 (180 steps x 0.015 = 2.7 seconds, assuming the servo can keep up with that)

If your servo is not working or running too fast, then try delay(333) // 180 * 0.333 = 60 seconds

Also, are you hoping the servo will run continuously? That is a different servo type.


Thanks for your reply. The servo sweeps 0-180 when it is high properly but stops as soon as the sound is gone.

I am trying to get it to keep moving for about 60 seconds more before stopping
 
Back
Top