Hi,
I am designing a robot to turn when it senses an obstruction in its path. I have gotten in it to move forward and stop when it senses an object 5cm away.
My issue now is that I have coded a micro servo that will be attached to the ultrasonic sensor.
This is used so that once the obstruction is seen the robot will then look for other paths (left and right). The micro servo looks both left and right but does not pause and look to see if it can advance forward in that direction.
How would I/should I code the components so that when the robot looks left and senses that there is not an obstruction 5cm it would turn right and vice versa for the left direction.
I have attached the code file to this posting
servo_both_ultra_working_SHIELD.ino (1.07 KB)
I'd like to see your code.
1 Like
TheMemberFormerlyKnownAsAWOL:
I'd like to see your code.
Ah, I'm bored...
// This code incorporates the use of two servos, ultrasonic sensor and a sensor shield. This eliminates the use of a breadboard
// The ultrasonic sensor senses an object 5cm away and then sends a signal telling the servo motors to stop
#include <Servo.h>
const int trigPin = A0;
const int echoPin = A1;
Servo servoR;
Servo servoL;
long duration;
int pos = 0;
int distance;
void setup() {
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
servoR.attach(9);
servoL.attach(10);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
// MAKING THE ROBOT STOP
if (distance <= 5) {
Serial.println("The robot will stop");
servoR.write(90);
servoL.write(90);
}
else {
servoR.write(180);
Serial.println("The robot will move forward");
servoL.write(-180);
}
delay(10);
}
1 Like
I believe I have attached it to the original posting
But here it is:
//This code utilizes two servos to move the robot
//an ultrasonic sensor, a sensor shield
//and one servo to turn the ultrasonic sensor
//This code is designed so when the robot senses
//an obstruction it will then look right and left
//to determine its new path
//ITERATION:
// The micro servo turns left and right but
// the wheels do not turn left and right
#include <Servo.h>
const int trigPin = A0;
const int echoPin = A1;
Servo servoR; // servo that contorols the right wheel
Servo servoL; // servo that controls the left wheel
Servo servoU; // servo that controls the ultraspnic
long duration;
int pos = 0;
int distance;
void setup() {
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
servoR.attach(9); //attach the servo port 9
servoL.attach(10); //attach the servo port 10
servoU.attach(11); //attach the servo port 11
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// MAKING THE ROBOT STOP
if (distance <= 5) {
Serial.println("The robot will stop");
servoR.write(90); // The right servo will stop
servoL.write(90); // The left servo will stop
servoU.write(0); // The microservo will turn to the left
delay(50);
if (distance > 6) { // The left distance will be calculated
servoR.write(0); // the right servo will turn left
servoL.write(90); // the left servo will not move
Serial.println("The robot will turn to the left");
}
else {
servoU.write(180); // the ultrasonic will turn the right
delay(50);
if (distance > 6) // the servo will check to see if the distance is greater than 6cm
servoR.write(90); // the right servo will not move
servoL.write(0); // the right servo will move
Serial.println("The robot will turn to the right");
}
}
else {
servoR.write(180); // The right servo to rotate
servoL.write(-180); // The left servo to rotate
Serial.println("The robot will move forward");
}
delay(100);
}
1 Like
But you didn't post it.
I wonder why.
But not very much
And what do the debug prints tell us?
One means that the robot will continue to move forward in a straight path
The robot will stop moving entirely to check for distances
The robot will turn to the left because there is nothing obstructing its path 5cm away
The robot will turn to the right because there is nothing obstructing its path 5cm away
And what do the debug prints tell us?
And what is told before things stop working? We don't have Your setup so we can't run it.
Hi,
Just for another perspective, here is an example of a robot that does that kind of collision avoidance:
https://arduinoinfo.mywikis.net/wiki/RobotKitMenu-8