Obstacle Avoding Line Following Robot

Greetings,
I made a line following robot and there is a servo motor in front of the robot and a digital 10cm IR sensor on servo motor. My aim is to avoid the robot from obstacles and find the lineonce again and follow the line. Obstacles will be on the line and I want the servo motor turn 60 degree left if left side is empty robot will make a semi square to avoid obstacle or when left side is not empty servo will turn to the right if right side is empty robot will make a semi square or something to avoid obstacle. But there will be also an obstacle when the robot avoiding from first obstacle. What should I do?

Thank you very much.

I attached my code and the pictures of my robot.

Link of my distance sensor.

my robot's code.txt (2.52 KB)

Obstacles will be on the line and I want the servo motor turn 60 degree left

How will you determine when the 60 degree turn is complete?

robot will make a semi square

What does this mean? The robot can only move forward, turn left, and turn right. Describe what you want to do in terms of its capabilities.

But there will be also an obstacle when the robot avoiding from first obstacle. What should I do?

Go into battle bot mode, and shove it out of the way.

When I give 60 with PWM output servo motor will turn to 60 degree such as analogWrite(10, 60) servo will turn to 60 degree, 10 is the digital PWM output.

When my robot see an obstacle firstly it should look to left with 60 degree and right to 60 degree, I mean servo will be initially at 90 degree and when sensor see an pbstacle robot will stop and servo turns to 30 degree and 150 degree than decide which way is empty. Such as right side is emty and robot will turn 90 degree right than goes such as 20 cm than turns 90 degree left and goes 20cm and turns 90 degree left and goes 20cm and find the line. Also when robot is making these actions it should look at also to obstacles and to black line. I hope I am clear now. Thank you for your return Sir.

When I give 60 with PWM output servo motor will turn to 60 degree such as analogWrite(10, 60) servo will turn to 60 degree, 10 is the digital PWM output.

Why are you using analogWrite() to control a servo? That is the wrong way to control a servo, normally. What kind of servo is it, and why are you not using the Servo library?

Such as right side is emty and robot will turn 90 degree right than goes such as 20 cm than turns 90 degree left and goes 20cm and turns 90 degree left and goes 20cm and find the line.

How do you measure distance?

Yes you are right I should use Servo Library.

Let me start from another point and step by step, I write a code that when the sensor see anything motors will stop else follow the line and the code is rob3.txt

When I add something after set-motors(0,0)

such as
if (sensorValue= analogRead(A5) < 500)

  • {*
  • set_motors(0, 0);*
  • Serial.println(sensorValue);*
  • }*

I can not see the sensorvalue on serial monitor and my robot stops when sensor see obstacle, but when I do not show anything to sensor robot do not move instantaneously it stops some time.

Moreover I can not find any place to write myservo.attach(10) to define the output to the servo.

rob3.txt (2.73 KB)

When I add something after set-motors(0,0)

such as
if (sensorValue= analogRead(A5) < 500)
{
set_motors(0, 0);
Serial.println(sensorValue);
}

So, analogRead() is called to read the value of A5. That value is compared to 500. The result of that comparison, true or false, is assigned to sensorValue. If the value read from the analog pin is greater than 500, the block will be skipped, and nothing will be printed. Is that what you want? Printing true or false seems silly, to me.

Operator precedence rules need to be studied, or parentheses need to be added to override them.

Greetings, I have changed my code like below and my robot will stop when it see an obstacle , then my servo sweeps 90->150, 150->30, and 30->150 . There is a problem I have now, such as the IR sensor does not see anything when servo is after 130, sensor sees obstacle between 30-130, when there is an obstacle on 90 degree of servo servo starts to move ance again to 150 and 30 and 90. I want my loop to turn once and gives the information which degree has not an obstacle. I hope I was clear.

void loop()
{

unsigned int sensors[5];
int position = qtrrc.readLine(sensors);
int error = position - 2000;
myservo.attach(10);
int pos = 90;
int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;

int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;

if (sensorValue= analogRead(A5) < 500)
{
myservo.attach(10);
set_motors(0, 0);
int pos = 90 ;

for(pos = 90; pos < 150; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 150; pos>=30; pos-=1)
{
myservo.write(pos);
delay(15);
}
for(pos = 30; pos<=89; pos+=1)
{
myservo.write(pos);
delay(15);
}
}

void set_motors(int motor1speed, int motor2speed)
{

if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
motor1.setSpeed(motor1speed); // set motor speed
motor2.setSpeed(motor2speed); // set motor speed
motor1.run(FORWARD);
motor2.run(FORWARD);

}

When you attach() a servo, it stays attached. Why do you keep attaching the servo?

if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0

There is a constrain() function...

If you are not going to read data from the servo while waving the servo around, does it make sense to wave the servo around?