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?
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.
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.
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 ;