Ultrasonic sensor Controlled Servo

Hi,

I am trying to use an Arduino UNO to control 2 Hitec Servos via an Ultrasonic Sensor. But the servos are not responding to the ultrasonic sensor. Is there a problem with my sketch or the processing is too much for an UNO ? TQ

Here's my sketch :

// defines pins numbers
const int trigPin = A1;
const int echoPin = A2;

#include <Servo.h>
Servo myservo;
Servo myservoA;
int pos = 0;
int posA = 0;

// defines variables
long duration;
int distance;

void setup() {
myservo.attach(9);
myservoA.attach(10);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

if (distance < 10)
{for (pos = 0; pos <= 40; 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(50);
{for (posA = 0; posA <= 40; posA += 1)
myservoA.write(posA); // tell servo to go to position in variable 'pos'
delay(50);
}

if (distance > 10)
{for (pos = 40; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50);
{for (posA = 40; posA >= 0; posA -= 1) // goes from 180 degrees to 0 degrees
myservoA.write(posA); // tell servo to go to position in variable 'pos'
delay(50);
}
}
}
}

No, the processing is not too much.
Does the servo sweep sketch work?
What do your debug prints tell you?

Please remember to use code tags when posting code

for (pos = 0; pos <= 40; pos += 1)  // goes from 0 degrees to 180 degreesComments that don't match code are a waste of space.

Are you sure you really want those servo for loops nested like that?

Steve

Nested?

Could be the wrong word. I really mean the braces in the servo bit need sorting out. I can't quite work out exactly what's going on but I doubt if it is what the OP intended.

Steve

if (distance < 10)
{for (pos = 0; pos <= 40; 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(50);
    {for (posA = 0; posA <= 40; posA += 1)
    myservoA.write(posA);              // tell servo to go to position in variable 'pos'
    delay(50);                
}

So if the distance is less than 10 you have a for loop that runs the pos variable up to 40 as fast as it can possibly go and then delays for 50 ms and repeats. I doubt the servo actually has time to move back to zero in the few ms it takes for that for loop to run it back up to 40. Were the delay lines supposed to be part of the for loops? That would make more sense. If so then you need to check your braces.

Ever see how real programmers format their code with all the braces on their own lines and the code blocks indented nice and neat? We don't do that just to make things pretty. We do that so we can see which block is which easily. Hit Control-T on your code to autoformat it and see if you don't spot the error a little easier.

And note that the next if (distance > 10) is inside the scope of the original if (distance < 10). Since that test only runs when distance < 10 I can pretty much guarantee that it will never be > 10 at the same time.

Steve