Let's Make Robots!

How do you get your bot to choose the right direction?

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;

}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
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.
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.

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;

}

In the switch statement replace myDistance[14] with rightDistance and myDistance[0] with leftDistance

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]);

 

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)':

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.