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.
