hello gugs,
Im new to this arduino platform. I need help constructing a code for making a continuous rotation servo spin in one direction when an object is within 10cm of an ultrasonic sensor and in opposite direction when object is moved away. At the moment the servo only changes direction once and doesn't happen again after that. Please help!This is the code i have so far:
I am very new to this and still getting to grips with the code language. I will read up some more to see if I can provide you with any more information.
void loop()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
long distance = (duration/2) / 29.1;
Serial.print(distance);
Serial.println(" cm");
if (distance<=5){
myservo.write(TRIGGERED_SERVO_POS);//turn servo in one direction
}
else{
if distance>5{
myservo.write(DEFAULT_SERVO_POS);// turn servo in opposite direction
delay(500);
}