# Trying to compare two direction

What I’m wanting to do is take the leftdistance and rightdistance and see which have a greater distance. Right now it choose right no matter what. So does that mean its just not holding the two distance to compare. Anyone have any suggestion how I could do this.

``````void decide()
{
int i = 0;

for (myAngle = 15; myAngle <=155; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(80);
mypath(myAngle, mydistance[0], mydistance[14]);
}
for (myAngle = 155; myAngle >=15; myAngle-=10)
servoPulse(myservo, myAngle);
delay(80);
mypath(myAngle, mydistance[0], mydistance[14]);
}

void mypath (int myAngle, int leftDistance, int rightDistance)
{
switch(myAngle)
{
case 15:
if (leftDistance > rightDistance)
{
left();
}
break;

case 155:
if (rightDistance > leftDistance)
{
right();
}
break;
}
}
``````
``````for (myAngle = 15; myAngle <=155; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(80);
mypath(myAngle, mydistance[0], mydistance[14]);
}
``````

On the first time through this loop, what is the value of “mydistance [14]” ?

Also, may want to perform a pre-decrement rather than a post.

its a sharp gp2d120 that read 4cm - 30cm. I was trying to see which have the greater distance and turn that way. So I’m doing something wrong then.

? I thought I would have to say leftdistance = 5in and rightdistance = 5in but that wouldn’t sound right. I know its just looking left and right and not taking any reading down so therefor its jumping to case155 and turning right. So what would be the best and easliest way of doing this.

You’re all over the place, aren’t you demonic_crow.

AWOL has it nailed with

On the first time through this loop, what is the value of “mydistance [14]” ?

If you haven’t initialized your variables (loop through the array and set them all to something like zero,) who knows what value is in mydistance[14]

Also, the sharp doesn’t simply return a nice linear value based on the range. The value from analogRead needs to be converted.

The other one is, that end of the first “for” loop, the index “i” has a value of 15.
In the second “for” loop’s first pass, you use this value as the index into “mydistance” - unless “mydistance” is declared large enough, this will be an out-of-bounds index, hence my comment about “pre decrement” instead of “post decrement”

don’t really know what your talking about I did change it to pre instead of post. All I was trying to do was just take angle 15 and compare it to angle 155 then see which have the greater distance and turn that way. I figure by having the mydistance[++i] = analogRead(0) would give them the values. I guess it see the value but doesn’t hold it in long enough to get to the value to compare. So guess I’m way over my head on this then, thanks.

Edit: I was trying to get it to look left and have it store the distance, then look right and have it store the distance then see if leftdistance is > then rightdistance.

“++i” is “pre-increment”, not “pre-decrement”.
The problem is in the second “for” loop, not the first.

If you’re only interested in the readings for 15 and 155 degrees, why bother saving all the other values?

Yea all I needed was the 15 and 155. Someone told me before to place it in an array. So I did remove the second for loop since I only needed it to look left then right.

OK - Sometimes when people post code, it’s a work-in-progress, and they’ve got a point where they want to get to, and make a improved more complex algorithm. Then there are bits of stuff that don’t yet do anything or don’t work at all, and they just cloud the key issue.

That’s fine, yes, just do two positions (don’t forget to allow the servo time to move!), forget the array and put the results into two variables - much clearer and much easier to debug.

Good luck!

``````void servoPulse(int myservo, int myAngle)
{
pulseWidth = (myAngle * 10) + 600;
digitalWrite(myservo, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(myservo, LOW);
}

//
// Forumla from Acroname robotics
// http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
//
int convert_gp2d120_range(int raw)
{
return (2914 / (raw + 5)) - 1;
}

void decide()
{
// look left
servoPulse(myservo, 10);
delay(500);

// look right
servoPulse(myservo, 160);
delay(500);

// move
mypath(leftDistance, rightDistance);

}

void mypath (int leftDistance, int rightDistance)
{
if (leftDistance > rightDistance)
{
left();
}
else if ( rightDistance > leftDistance )
{
right();
}
else
{
// what do you want to do if they're equal?
}
}
}
``````

This doesn’t work for me either :(. All it does is barely even look left and right. Anything I can improve upon. Thanks a bunch. Sorry its just this one little function I’m having problem with and everything else is fine.

Rather than control the servos yourself, it’s a lot easier to use the libraries:

http://arduino.cc/en/Reference/Servo
or
http://www.arduino.cc/playground/Code/MegaServo
Check out the “sweep” example to see how to use them - and note that the first library only controls servos on pins 9 and 10.

Those half second delays may not be long enough to allow the servo to sweep full travel.

I already have servos on pin 9 and 10. So I have this one on pin 2. Not to sure how I would go about using it. I did see something about the attached and if you use it on pins other the 9 and 10 you have to do a refresh rate I think. Still didn’t make much sense to me. I wish all the pins could be as easy as pin 9 and 10.

Use the megaservo library.

Thank you I just updated my arduino and added the MegaServo lib this morning, but didn’t think about trying it. I did see if it would control a the servo on pin 2 and it did. So I’m going to rewrite some of my code to work with the MegaServo and see if I have any problems. Thanks for your help.

``````void lookleft()
{
}

void lookright()
{
}

void decide()
{
lookleft();
delay(400);
lookright();
delay(500);

if (myleft > myright)
{
left();
}
else
{
right();
}
}
``````

I had wrote this so many different ways and this was my last built. I don’t know what I’m missing. It still just jump to the very end of my if statement or switch/case statement no matter what. So it just forget about the left distance. My mind is just a little blank now.