I have been trying everything to figure away to get my robot to choose the direction that have the greater distance. I can't seem to figure out whats wrong. I tried to figure something out. One time I wrote it different and it would wait till it got to the last case and turn right no matter what. This is my code.
void decide()
{
int i = 0;
for (myAngle = 15; myAngle <=155; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(80);
mydistance[i++] = analogRead(0);
mypath (myAngle, mydistance); says invalid conversion from 'int*' to 'int' in function 'void mypath(int,int)'
}
}
void mypath (int myAngle, int mydistance)
{
switch (myAngle)
{
case 15:
if (mydistance[0] > mydistance[14]
{
left();
}
break;
default:
right();
break;
}
}
also I had it like this:
void mypath (int myAngle, int mydistance)
{
switch(myAngle)
{
case 15:
if (mydistance[0] > mydistance[14]
{
left();
}
break;
case 155:
if (mydistance[14] > mydistance[0]
{
right();
}
break;
}
mydistance[i++] =
mydistance[i++] = analogRead(0);
mypath (myAngle, mydistance);
Hi, mydistance appears to be an array in the first line, but when you reference it in the 2nd line you haven`t told it which bit in the array.
Try changing it to mypath(myAngle, mydistance[i]); and see if it works.
It
It highlighted: #include <Servo.h>
then give me the error:
error: ‘i’ was not declared in this scope In function ‘void decide()’:
At global scope:
In function ‘void mypath(int)’:
Passing arrays versus passing single int
Your function:
void mypath (int myAngle, int mydistance)
Is expecting mydistance to be a single int, not an array of ints.
Changing it to:
void mypath (int myAngle, int *mydistance)
will allow you to pass in the whole array. Another thing to do is just pass the two values from the array you are working with:
void mypath (int myAngle, int leftDistance, int rightDistance)
then call it like:
mypath(myAngle, mydistance[14], mydistance[0]);
Is this what your talking about?
void decide()
{
int i = 0;
for (myAngle = 15; myAngle <=155; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(80);
mydistance[i++] = analogRead(0);
mypath(myAngle, mydistance[0], mydistance[14]);
}
}
void mypath (int myAngle, int leftDistance, int rightDistance)
{
switch(myAngle)
{
case 15:
if (mydistance[0] > mydistance[14])
{
left();
}
break;
case 155:
if (mydistance[14] > mydistance[0])
{
right();
}
break;
}
Close
In the switch statement replace myDistance[14] with rightDistance and myDistance[0] with leftDistance
Ok now its going back to the
Ok now its going back to the issue again how I had it some other time. Now it seem like it run the scan and then turn right no matter what.
Serial.println is your friend
Take a look at what the values are that are getting passed in. That will help determine which variable doesn’t have the value you think.
the way I have it is it even
the way I have it isnt it even able to hold the left value until it get to the right to make a choice. To me it just seem like its just looking left and right and running my switch and jumping straight to case 155 since its at the end and turn right. So that does mean its not holding my distance for left and right then? If thats the case how would I get it to hold the distance long enough for it to compare with the other distance.
Some problems I see
The sharp sensor won’t just return a nice value from analogRead that represents the distance. You need to apply some math to get the value.
These will help you out with that:
http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
http://www.arduino.cc/playground/Main/ReadGp2d12Range <- you’ll need to change the formula in this a bit for the 120
Also, skip the whole scanning stuff for now. Move the servo to the right, take a reading and store it in a variable called rightDistance. Then, move the servo left, read the distance and store it into a variable called leftDistance. Then, your myPath routine comes down to which has a greater distance, right or left. Once you get that working, you can start to get fancy with the scanning.
This isn’t teaching you to fish but maybe it will get you closer
//
// 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, 15);
delay(80);
int leftDistance = convert_gp2d120_range(analogRead(0));
// look right
servoPulse(myservo, 155);
delay(80);
int rightDistance = convert_gp2d120_range(analogRead(0));
// 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?
}
}
}
Well this code didn’t
Well this code didn’t work. It doesn’t even go to 10 deg and 160 deg. When it stop it verly look left and the right and just turn right. I guess I just hit a brick wall for now and going to be stuck at this one function. Thanks for trying to help though.
**void lookleft(){ **
void lookleft()
{
headservo.write(10);
myleft = analogRead(0);
}
void lookright()
{
headservo.write(160);
myright = analogRead(0);
}
void decide()
{
lookleft();
delay(400);
lookright();
delay(500);
if (myleft > myright)
{
left();
}
else
{
right();
}
}
It still want to wait till the end of the if statement to make a right turn the same way with a switch statement. What am I’m missing here.
Not following…
What do you mean wait until the end of the if statement?
The code looks sound (assuming that myLeft & myRight are global variables.)
It seem to not even pay
It seem to not even pay attention angle 10. So what I’m sayin is all its doing is when it go back to 160 deg it just jump to the very end of my if statement and runs it. So it just seem like it not even comparing them at all. What I figure you would have to do is
Look Left take left reading and store it my left
look right take right reading and store it my right
then compare left > right
turn Left
All its doing is look left and look right then just decide, thats what I’m guessing. Am I’m right or wrong? Sorry I 'm a bit lost.
Reading from the Sensors
The code looks mostly sound with one exception. Are you doing more than just an analogRead() to get the value? If not, the results aren’t what you think.
From http://www.acroname.com/robotics/info/articles/sharp/sharp.html#e26
You will have a higher value for closer objects. Are you trying to get closer or move away?
Another thing to try is read the left first.
When object is in front I
When object is in front I want it to back up then look left and then look right. I want it to choose the direction that have the greatest distance and turn that way. Also yes thats my code to do this stuff with. I was going to have the robot just choose a random direction but figure this would be way better. So am I’m missing alot in my code or just a few things. Sorry like I said before I am still learning to program and understand stuff. You have been a really big help though, thanks.
Greater Distance
The lower value will actually be the greater distance if you are just doing a simple analogRead()
Try swapping:
myleft > myright
to be:
myleft < myright
Outputting values from the sensor to the Serial port is another good way to test what you are getting back. Just remove the power to the wheel motors/servos so it can stay tethered to the PC.