Hello, I'm making an obstacle-avoiding robot using Arduino Uno, a Motor board, 2 servos, and an ultrasonic sensor. My senor won't trigger my servos to move backward and my servos twitch. What do I need to change?
#include<Servo.h>
const int trigpin = A0; // Trigger pin
const int echopin = A1; // echo pin
float duration, distance;
Servo myservo1; // Left leg
Servo myservo2; // Right leg
int pos = 0;
int pos1= 100;
int pos2 = 90;
int pos3 = 80;
void setup() {
Serial.begin(9600);
myservo1.attach(9);
myservo2.attach(10);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
}
void loop ()
{
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance = duration*0.034/2;
Serial.print(distance);
Serial.println("cm");
delay(100);
if (distance <= 10)
{
Serial.print("distance is less than or equal to");
myservo1.write(pos1);
delay(100);
myservo1.write(pos2);
delay(100);
myservo2.write(pos1);
delay(100);
myservo2.write(pos2);
delay(100);
}
else {
myservo1.write(pos3);
delay(100);
myservo1.write(pos2);
delay(100);
myservo2.write(pos3);
delay(100);
myservo2.write(pos2);
delay(100);
}
}
Yes, I would expect the servos to twitch. A lot.
First, general notes on you coding.
Use CTRL-T to format your code in the IDE. It makes it easier to read.
Use descriptive names. For example,
Why not?:
Servo leftLeg;
Servo rightLeg;
This would also make the code more readable. As would comments.
Why would I expect a lot of servo jitter? You are writing something to the servos all the time. Hundreds of times per second. You have no hysteresis in the distance, so a difference of .0001 inches from the prior loop would be significant.
No, that was just a suggestion to make the code easier to read.
There is nothing in your code that says "I don't need to move". So with every loop(), I.E. several hundred times per second, you are sending new commands to the servos. (I would be very surprised if the distance sensor didn't jitter a few thousandths of an inch even when not moving).
I would recommend that you test the distance from the prior distance before if (distance <= 10)
and only send the servo commands if the distance has changed by a distance threshold you are comfortable with. (I don't know the resolution or accuracy of your mystery distance sensor).
before going back to writing code there should be a normal-word-description what you want the obstacle-avoiding-robot to do.
what do you mean by "trigger my servos to move backward" ?
Are the two servos continous rotating servos?
How should the robot drive if the robot has detected an obstacle ?
Should the robot whirl highspeed in circles until the robot is drilling itself into the ground?
surely not!
But what shall he do then?
turning 90 degrees? turning 180 degrees? then move back?
first move back? if yes what distance shall he move back?
after that turning 90 degrees?, turning 180 degrees? Turning to the right? turning to the left?
You should divide the whole thing into smaller parts.
first part:
if an obstacle is detected simply stop the motors. Not more not less.
"testing the distance" means writing an if-condition that is comparing actual measured distance with a pre-defined threshold-value.
Before you ask
"OK How would I write an if-condition that is comparing actual measured distance with a pre-defined threshold-value?"
Well As long as you don't have a basic understanding of such things as variables, functions, if-conditions etc. you should learn the real basics
Take a look into this tutorial:
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
If this is your really first project with programming at all it is ambitious.
next part: simply move backwards for a short distance not more not less
next part turn etc. etc.
Be the change you want to see in the world
best regards Stefan
I've been on that tutorial and even took notes so I do have an understanding of Arduino programming it is just I can't figure out why my robot won't function. I want my sensor to activate the servos so they can move back when near an obstacle and I want my servos to stop twitching.
Of course the code that you posted shows that you have quite some understanding how programming works.
But to be really honest: your knowledge about programming is pretty limited.
This is really OK. Everybody here had a time in his/her life where she/he did not know a single detail about programming.
Just trying to pretend something similar as
"I know 90% of it can you give me a 8% hint so I get it to work"
while you know only 20% gives a bad impression.
Your code does exactly what you have coded
example:
myservo1.write(pos1); // set servo to a position
delay(100); // wait 0,1 seconds
myservo1.write(pos2); // set servo to a NEW position
delay(100); // wait 0,1 seconds
myservo2.write(pos1); // set servo to a NEW position
delay(100); // wait 0,1 seconds
myservo2.write(pos2);
delay(100);
what do you expect other than twitching if you change a servo-position 10 times per second!!??
You can go on posting global but short descriptions like
hoping someone will post a working code for you.
But this is very unlikely to happen. Most users will not answer because you show too less own effort by asking such global questions.
If you show more effort through a new version of your code and then ask a specific question
the answers will rush in.
Even the most professional software-developer will break down the project "obstacle avoiding robot" into small parts like
detecting too short distance
stopping the motors
reversing motordrive-direction
because this is the professional way to do it
You have still a lot of to learn. If you are willing to do smaller steps and ask specific questions you have the support of many users.
Be the change you want to see in the world
best regards Stefan
I can see clearly why the servos are twitching but I don't know how the servos mechanically accomplish the backward motion. A description of the mechanical aspect of your project might help or a description of the servo motion required to accomplish a backward movement.
Servos would go 80 - 90 degrees and 90 - 80 degrees to go backward motion. Once the robot is close to an object the if statement should kick in making the robot to move backward
You are using #include<Servo.h> and making instances of Servo, why not use #include<SR04.h> (github link below) and make an instance of Sensor to make triggering, time and distance easier to use until robot parts are moving correctly. When using Servo.h, it's a one-liner to do the work of the seven lines you have at the top of loop(). And, you asked, "How do I test the distance." You already do, in the first seven lines of the loop() function.
I don't think you need type const int trigger or const int echo pins... a global #define TRIGPIN and #define ECHOPIN will do.
I agree with SteveMann's servo instance naming for removing any confusion of servo to move.
Why not "pos0" for zero degrees?
And finally... you say "forward works" (pos1, pos2, pos1, pos2). Why not swap pos1 and pos3, making "forward" pos3, pos2, pos3, pos2 and "backward" (which currently does not work) pos1, pos2, pos1, pos2). That might help you see if the servo is just broken or your robot is making "backward" inoperable.