Let's Make Robots!

funny problem with easy obstacle avoiding robot

Well I am making an obstacle avoiding robot with my friends its for a competion to win 2000 dollars. 

I programed the robot and everything It works pretty good, whenever the robot "sees" an obstacle it moves a servo with a sensor right and left and decides which way to go.

The problem is that it has to got through a course which has 11 90 degree turns I attached a pdf with rules and diagram. 

So whenever the robot sees something it moves its head and goes towards the correct direction as expected but then even though there is nothing in front of it, the robot moves its head again unnecessarily and goes a different direction but these happens only like 8 out of 10 times and while the robot moves through the course the person cant touch it. Please help I have to win :) 

here is the code;

 

#include <Servo.h> 
int right1 = 11; 
int right2 = 12; 
int left1 = 6;
int left2 = 7;
Servo anshul;
int pos = 0;
int sensor = A0;
int object = 150;
int rightval,leftval,frontval;

void setup() {
 
    pinMode(right1, OUTPUT);
    pinMode(right2, OUTPUT);
    pinMode(left1, OUTPUT);
    pinMode(left2, OUTPUT);
Serial.begin(9600);
anshul.attach(5);
anshul.write(90);
}

void loop() 
{
/*frontval = analogRead(sensor);
Serial.print(frontval);
Serial.print("\t");
Serial.print(rightval);
Serial.print("\t");
Serial.print(leftval);
Serial.println();
*/

if(frontval < object)
{
 forward();
 anshul.write(90);
 }
else//if(frontval > object)
{

stop();


  anshul.write(0);
 delay(500);
checkright();
 delay(500);
 anshul.write(180);
  delay(700);
  checkleft();
  delay(500);
  anshul.write(90);
   delay(100);
   comparedistance();


}
} 

void checkright()
{
  rightval = analogRead(sensor);
}
 void checkleft()
{
  leftval = analogRead(sensor);
}

void comparedistance()
{
  if(rightval<leftval)
  {
    right();
    delay(270);
  }
  if(leftval<rightval)
  {
    left();
    delay(270);
  }
}
 








void right()
{
  digitalWrite(right1, LOW); 
  digitalWrite(right2, HIGH); 
  digitalWrite(left1, LOW); 
  digitalWrite(left2, HIGH); 
}

void left()
{
  digitalWrite(right1, HIGH); 
  digitalWrite(right2, LOW); 
  digitalWrite(left1, HIGH); 
  digitalWrite(left2, LOW); 
}
void backward()
{
  digitalWrite(right1, LOW); 
  digitalWrite(right2, HIGH); 
  digitalWrite(left1, HIGH); 
  digitalWrite(left2, LOW);
}
void forward()
{
   digitalWrite(right1, HIGH); 
  digitalWrite(right2, LOW); 
  digitalWrite(left1, LOW); 
  digitalWrite(left2, HIGH);
}
void stop()
{
   digitalWrite(right1, LOW); 
  digitalWrite(right2, LOW); 
  digitalWrite(left1, LOW); 
  digitalWrite(left2, LOW);
}

AttachmentSize
https___mail-attachment.googleusercontent.com_attachment_u_0__ui2ik8c5c1f4b4cviewattth13ee923cdead7e83attid0.pdf885.9 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
robotmaster18's picture
I got it working now it was pretty easy I just had to put a forward after the right or left in the part that says void comparedistance We might actually win now since the other contestants are using the companies products which are pretty bad.
lumi's picture

Good to know that you finally got it. Please let us know about the competition and if you won and if you not, why? Such informations are always welcome since many people are facing the same problems as you just did.

Oh...almost forgot...as you probably know...we have an un-written rule here:

If you get some winnings with the help of other fellow LMRians then this help should be rewarded by a donation (any amount above 1% of the winnings) to this awesome website (Link for donations are in the top navigation menu). ;-) That's not a must but i am sure the webmaster will appreciate it :-)

Chris the Carpenter's picture

When you tested your sensor by itself, what kind of numbers were you getting from it? --As bdk asked, and I will ask again, and is incredibly important, and that you should have added, is, what kind of sensor are you using?

I am going to guess it is a "standard" sharp sensor. I am also going to guess that 150 is probably too low a threshold. My Sharps seem to hover at 40-60 or so when seeing infinity, 150 has to be close to the limit of its range. 

Does this threshold corespond with your individual tests? When you did your sensor test initially, did you do any tests with the motors running?

What are your numbers?

robotmaster18's picture

first of all im really sorry that i randomly put the link in the chat box like an idiot. I was in a hurry

the values im getting with the sharp 2d120x are pretty straight forward 0-5 is when it sees to infinity and when youre hand is really close its about 450-500 so low value far distance less value closer distance is the general rule.

I thought 250 is about fine the 150 was for a test.

I did a test with the servo running since they share a power supply, It was pretty accurate. The motors have a seperate 12V so I did not do the test with motors.

 

P.S Look at the pdf it has the diagram of the course on it.

bdk6's picture

that isn't the code you are running is only wasting the time of people trying to help you.  By your own statements the code you posted is NOT the code you are running.  So you have people troubleshooting a program that isn't the one you are having trouble with.  

Please post the EXACT program you are running and the EXACT sensor you are using and the EXACT numbers you are getting.  The multi-page description of the contest is irrelevant.

robotmaster18's picture
Sensor is Sharp with a range of 4-30 cm
robotmaster18's picture

sorry that was not supposed to be commented out and anyway it was not commented out when i programmed the robot.

Another thing you should know is that the robot turns perfectly when there is a gap while it turns the 2nd turn on the map(look at pdf)

bdk6's picture

If I help you fix your problem how much of the $2000 do I get?

Not only have you not told us what kind of sensor you are using, but you have commented out the only place in your code that assigns a value to frontval (the first line of your loop() function.  )  Since it is a global variable it will be initialized to zero and stay there.