How do I make arduino car follow a premade route using arrays

void currentposition()
{
  for(int i =0; i<10;i++)
  { 
    for(int v = 0;v<10;v++)
    {
      if(maps[i][v] == 2 && maps[i][v-1] == 3)
      {
        maps[i][v] = 3;
        maps[i][v-1] = 0;
        forward();
        i = 0;
        v = 0;
      }
      else if(maps[i][v] == 2 && maps[i+1][v] == 3 && i == 0)
      {
        maps[i][v] =3;
        maps[i+1][v] =0;
        forward(); 
        stops();
        if(maps[i][v+1] == 2)
        {
          rights();
          
        }
        else if(maps[i][v-1] == 2)
        {
          lefts();
        }
        i = -1;
        v = 0;
      }
      else if(maps[i][v] == 2 && maps[i+1][v] == 3 && i == 9)
      {
        maps[i][v] =3;
        maps[i+1][v] =0;
        forward(); 
        stops();
        if(maps[i][v+1] == 2)
        {
          rights();
        }
        else if(maps[i][v-1] == 2)
        {
          lefts();
        }
        i = -1;
        v = 0;
      } 
      else if(maps[i][v] == 2 && maps[i+1][v] == 3 && maps[i-1][v]==0)
      {
        maps[i][v] =3;
        maps[i+1][v] =0;
        forward();
        stops();
        if(maps[i][v+1] == 2)
        {
          rights();
        }
        else if(maps[i][v-1] == 2)
        {
          lefts();
        }
        i = 0;
        v = 0;
      }
      else if( maps[i][v] == 2 && maps[i+1][v] == 3)
      {
        maps[i][v] =3;
        maps[i+1][v]=0;
        forward();
        i = -1;
        v = 0;
      }
      else if(maps[i][v] == 2 && maps[i][v+1] == 3)
      {
        maps[i][v] = 3;
        maps[i][v+1] = 0;
        forward();
        i = 0;
        v = 0;
      }
      else if(maps[i][v] == 2 && maps[i-1][v] == 3 && i == 0)
      {
        maps[i][v] =3;
        maps[i-1][v] =0;
        forward();
        stops(); 
        if(maps[i][v+1] == 2)
        {
          lefts();
        }
        else if(maps[i][v-1] == 2)
        {
          rights();
        }
        i = -1;
        v = 0;
      }
      else if(maps[i][v] == 2 && maps[i-1][v] == 3 && i == 9)
      {
        maps[i][v] =3;
        maps[i-1][v] =0;
        forward();
        stops();
        if(maps[i][v+1] == 2)
        {
          lefts();
        }
        else if(maps[i][v-1] == 2)
        {
          rights();
        }
        i = -1;
        v = 0;
      } 
      else if(maps[i][v] == 2 && maps[i-1][v] == 3 && maps[i+1][v]==0)
      {
        maps[i][v] =3;
        maps[i-1][v] =0;
        forward(); 
        stops();
        if(maps[i][v+1] == 2)
        {
          lefts();
        }
        else if(maps[i][v-1] == 2)
        {
          rights();
        }
        i = 0;
        v = 0;
      }
      else if( maps[i][v] == 2 && maps[i-1][v] == 3)
      {
        maps[i][v] =3;
        maps[i-1][v]=0;
        forward();
        i = -1;
        v = 0;
      }
      


    }
  }
}
void forward()
{
  Serial.print("Forward ");
  digitalWrite(MotorA1, LOW);
  digitalWrite(MotorA2, HIGH); 
  digitalWrite(MotorB1, HIGH);
  digitalWrite(MotorB2, LOW);
  delay(1000);
}
void stops()
{
  Serial.print("stop");
  digitalWrite(MotorA1, HIGH);
  digitalWrite(MotorA2, HIGH); 
  digitalWrite(MotorB1, HIGH);
  digitalWrite(MotorB2, HIGH);
  delay(500);
}
void lefts()
{
  Serial.print("left ");
    digitalWrite(MotorA1, LOW);
    digitalWrite(MotorA2, HIGH); 
    digitalWrite(MotorB1, LOW);
    digitalWrite(MotorB2, HIGH);
  delay(1000);
  
  if(heading == 1)
  {
    heading = 4;
  }
  else
  {
    heading--;
  }
}
void rights()
{
  Serial.print("right ");
  digitalWrite(MotorA1, HIGH);
  digitalWrite(MotorA2, LOW); 
  digitalWrite(MotorB1, HIGH);
  digitalWrite(MotorB2, LOW);
  delay(1000);
  if(heading == 4)
  {
    heading = 1;
  }
  else
  {
    heading++;
  }
}
void backwards()
{
  
}

// I made and want it to follow the path and using time to calculate the distance travelled, but currently have trouble making it move forward i used a 2d array to make a "virtual map" for the bot to follow. and 2 is the path it is supposed to follow and it will only be trigger the condition if the 2 is paired up with a 3(car position currently) , adjacent of the 2. so an example:
0,2,2,0
0,2,0,0
0,3,0,0 it would have to move forward by 2 then turn right.

Bad idea. Use an optical interrupter to count wheel motion; x counts per wheel revolution will give the circumference as a means of computing distance.

Please edit and fix your code with code-tags
image

before someone has a hissy-fit.

Thanks for the advice, however there is a slight problem I dont have anything to track the rotation of the wheel and the project is for my school and we are not supposed to buy any product or sensors to keep it fair for my class, I understand this might come off as a really dumb idea to start however it is too late for me to change my program and this is the last part that I need to finish, as the tracking of the path works fine.

If "travel time" works well enough as a substitute for distance, then store the appropriate times in the array, along with the turn information.

I don’t really understand what you mean by that could you clarify.

Stored path instructions for the vehicle require information on turns and travel distance.

If "travel time" works instead of distance, fine.

Ok so for my code the information it gets is that it should approximately travel forward for 1 second to get to the next grid and turn for 350 Milli seconds to turn 90 degrees, I am only making it very simple as in a turn is always 90 degrees, is that what you mean by the information I have to give the bot because I added that to my conditions according to where my 2 and 3 are positioned.

If a turn is always 90 degrees in the same direction, then you need to store only travel distance (or time).

Please explain what you mean by "where my 2 and 3 are positioned".

Ok so in my code I use for loops to check each row and column for 2s and it will only trigger an action if there is a 2(path it should take) with a 3(current position of the car) so an example would be
0,0,0
0,2,0
0,3,0
it would find the 2 in the array as there is a 3 around it either beside or above or below it another example would be
0,2,2,0
0,2,0,0
0,3,0,0
It will only find the 2 that has a 3 around it which is array[1][1] as array[2][1] is a 3 and this will continue the loop of finding a 2 with a corresponding 3 until there are no more 2 as it reached its destination
So the process would look like this
0,2,2,0
0,2,0,0
0,3,0,0
Then
0,2,2,0
0,3,0,0
0,0,0,0
Then
0,3,2,0
0,0,0,0
0,0,0,0
Then
0,0,3,0
0,0,0,0
0,0,0,0

please describe your path encoding scheme in English, non array, non-programming terms.

You will probably get a lower grade if you hand in the assignment without it anyway.

otherwise you will be the only person who will ever understand it

Ok so lemme explain in English
So it will scan the map basically the array and find the path it has to follow, that is the first step, since the path is made already it will then find the beginning of the path so basically where the car is and it will tell the car to do certain actions either moving forward only or move forward then turn Left or right, this will proceed on until it has reached the end of the path since there is no more path to follow it will eventually break the loop and then the car is in the new position. And you are able to put in another point that you want the car to go to. I hope this is better to understand.

absolutely not. you did not say anything about how the path is encoded (represented).

Also please use punctuation when you write.

Sorry, that makes no sense.

1 Like

Ok sorry about that i don’t really know what I need to put in to help y’all understand jst tell me I will try to help as I really need and appreciate the help y’all are giving

Best way to understand this, imagine you know nothing about the project. What would you need to read, to understand it?

Ok I will try my best, I have a 10 by 10 grid map, the distance between each coordinate is not important is it can change from 1cm per grid to 10cm per grid, and on this map I am trying to get the car to move from one position to the other. Basically how I do it is first by having the map point out where the car is currently positioned, then the person would put in a marker or point on the map that he wants the car to go to. Then I used an algorithm, a basic one, to determine how many units the path has to cover horizontally until it detects the position of the car in the same column. Then it will do that for the vertical finder and mark out a way for the car to move vertically until it reached the position of the car. So once it found the car it will trigger the next step which is to follow the path by detecting where the next point on the map and it will do this continuously until it reached the end of the path. I guess the best way to summarise it would be like a train following the track.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.