I am having issues having the servo turn when I clap. I am not sure if its because I need to use the analog out or If it is impossible to activate the servo with a digital read as input from the sound sensor. Maybe there is somethings wrong with my code. Will someone please help.
#include <Servo.h>
Servo tinoServo;
int servoPin = 9;
int soundSensor = 4
int val
void setup()
{
tinoServo.attach(servoPin);
}
void loop()
{
val = digitalRead(soundServo);
if (val == HIGH)
tinoServo.write(180);
delay(5000);
val = digitalRead(soundServo);
if (val == HIGH)
tinoServo.write(0);
delay(5000);
the logic is flawed... assuming your sound sensor is indeed sending "1", if you don't clap a second times exactly 5 seconds after the first time, but say 6 - then the second if will have been ignored and you returned in the loop cycle and you don't know which if will capture the next "clap" sound.
May be it will be the first one, and it will send again tinoServo.write(180); and your servo won't move.
What you want to do is probably have a suitable variable that stores the states you are in.
float servoPos = 0; // default position, the servo write takes a float angle [tt]
in [b]setup()[/b]
[tt]Move servo to servoPos default position
in loop()
if clap is heard then {
servoPos = 180.0 - servoPos;
set servo to servoPos position
}