I have made a bot using 3 ir sensors, l293d motor shield and arduino . I am facing problem regarding the selection of correct path in case of two path being presented to it.How will it choose which is the correct path.
What is happening is that due to overshooting it is getting in the straight path only but not turning, even if i implement the code to turn 180 degree at the end of the straight line it will turn,and still continue travel in the straight path , and in that way retrace the path ,not turning in the correct path .
I am talking about the case depicted in the image attached
If I had to wait five minutes between posts, I'd like to think that I would use them carefully, to consider what the people I'm asking (who are giving their time for free) need to know about my problem, to give them the best chance to provide a useful answer.
#include<AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int left;
int right;
int i = 0;
int center;
void setup() {
// put your setup code here, to run once:
motor1.setSpeed(150);
motor2.setSpeed(150);
pinMode(A1,INPUT);
pinMode(A5,INPUT);
pinMode(A4,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
left = analogRead(A1);
right = analogRead(A4);
center = analogRead(A5);
Serial.print("left");
Serial.println(left);
Serial.print("center");
Serial.println(center);
Serial.print("right");
Serial.println(right);
action1(left,center,right);
}
void turn(){
delay(1000);
left = analogRead(A1);
right = analogRead(A4);
center = analogRead(A5);
if(left < 500 && center < 500 && right < 500){
motor2.setSpeed(60);
motor1.setSpeed(60);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(180);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(1000);
left = analogRead(A1);
right = analogRead(A4);
center = analogRead(A5);
if(left < 500 && center < 500 && right < 500){
motor2.setSpeed(60);
motor1.setSpeed(60);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(180);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(1000);
left = analogRead(A1);
right = analogRead(A4);
center = analogRead(A5);
if(left < 500 && center < 500 && right < 500){
motor2.setSpeed(60);
motor1.setSpeed(60);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(180);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(1000);
left = analogRead(A1);
right = analogRead(A4);
center = analogRead(A5);
if(left < 500 && center < 500 && right < 500){
motor1.setSpeed(60);
motor2.setSpeed(60);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(2000);
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(700);
return;
}
}
else{
return;
}
}
else{
return;
}
}
else{
return;
}
}
void action1(int l, int c, int r){
if(l <500 && r < 500 && c > 500){
motor1.setSpeed(40);
motor2.setSpeed(40);
motor1.run(FORWARD);
motor2.run(FORWARD);
}
else if((l < 500 && c < 500 && r > 500) || r >500 && c > 500 && l < 500 ){
motor2.setSpeed(40);
motor1.run(RELEASE);
motor2.run(FORWARD);
}
else if((r < 500 && c < 500 && l > 500) || (l > 500 && c > 500 && r < 500)){
motor1.setSpeed(40);
motor1.run(FORWARD);
motor2.run(RELEASE);
}
else if(l > 500&& c > 500 &&r >500){
motor1.setSpeed(60);
motor1.run(RELEASE);
motor2.run(BACKWARD);
delay(60);
}
else {
motor1.run(RELEASE);
motor2.run(RELEASE);
if(l< 500 && c < 500 && r < 500){
turn();
}
}
return;
}
This is the code, The bot is running on white line in black arena, when its all three sensors come on the black arena(i.e. end of the line)
It first checks on one side by moving 30 degrees 3 times and when it does not find it it takes a turn of 180 degrees.
You could take out all the delays.
You could indent your code(that wouldn't do anything for the accuracy, but it would help others to be able to read it.)
Where's action2()? I'm sitting on the edge of my seat looking for action 2!
Numbers in variable names and function names are almost always a bad idea. As soon as you're tempted to put numbers in the names, stop and think about using an array or another variable.
The multiple repeated code snippets, like repeatedly reading the sensors, cries out for a function. Make small functions that do simple things - just read the sensors, just make decisions, just run the motors. Then use those functions everywhere.
You also introduce some subtle errors by reading the sensors in many places. If you put something in action1() after the call to turn() then the variables l/r/c will contain very old values - they may still have values from 30 seconds ago. Which version of the sensor values is "correct" at that point?
Robin's [url=https://forum.arduino.cc/index.php?topic=261445.0=Planning and Implementing an Arduino Program[/url] introduces a very simple way of organizing your program into functions.