Maze Solving Robot

I am working on maze solving using IR led sensor (it gives digital input to arduino), motor driver ic l293d and arduino uno (ATMEGA 328). It’s not running properly my hardware arrangement is ok and this is my code.
When i am storing character in an array my bot doesn’t follow line and sometimes it hangs. I think there is a bug in the code

// CODE FOR MAZE SOLVER

int stage=0;

char path[500]={};

int l=0;

void setup()
{
// INITIALISATION OF ALL PINS
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(8,OUTPUT);
pinMode(9,INPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(7,OUTPUT);
Serial.begin(9600);
}

void loop()

{

// this read the IR array sensor
int a1=digitalRead(5); // a1 reads the value of IR sensor1

int a2=digitalRead(6); // a2 reads the value of IR sensor 2

int a3=digitalRead(2); // a3 reads the value of IR sensor 3

int a4=digitalRead(3); // a4 reads the value of IR sensor 4

int a5=digitalRead(4); // a5 reads the value of IR sensor5

int a6=digitalRead(9); // a6 reads the value of IR sensor6

digitalWrite(7, HIGH);

// SEARCHING THE MAZE OR SEARCHING THE END POINT OF MAZE

// it follow the line simply

if(stage==0)
{

if(a1 == 0 && a3 == 0 )
{

digitalWrite(8, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
if(path[l-2]==‘B’)
{
Shortpath();
}

path[l]=‘S’;// STORING THE STRAIGHT TURN

l++;

Serial.println(“breakpoint 1”);

}

if( a5 == 0 && a6 == 0 )

{
digitalWrite(8,LOW); /* code for turning motor left
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, LOW); */

path[l]=‘L’; // STORING THE LEFT TURN

l++;

if(path[l-2]==‘B’)

{
Shortpath();
}

Serial.println(“breakpoint 2”);
}

if( a4 == 0 && a2 == 0)

{
digitalWrite(12, LOW);
digitalWrite(8, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, LOW);

path[l]=‘R’;//STORING THE RIGHT TURN

l++; // ARRAY INCREMENT

Serial.println(“breakpoint 3”);

if(path[l-2]==‘B’)
{
Shortpath();
}
}

if(a1==1 && a2==1 && a3==1 && a4==1 && a5==1 && a6==1)

{

digitalWrite(12, LOW);
digitalWrite(8, HIGH);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);

path[l]=‘B’; // STORING THE TURNAROUND MOVE

l++;

Serial.println(“breakpoint 4”);
}
if(a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0 && a5 == 0 && a6 == 0)
{
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
Serial.println(“breakpoint 5”);

if(a1==0 && a2==0 && a3==0 && a4==0 && a5==0 && a6==0)
{

digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
stage==1;
replay();
Serial.println(“breakpoint 6”);
delay(2000);
}
}
}
}
// SHORTENING THE PATH OR REDUCE THE UNNECESSARY MOVE
void Shortpath()
{
int shortdone=0;
if(path[l-3]==‘L’ && path[l-1]==‘R’)
{
l-=3;
path[l]=‘B’;
shortdone=1;
}

if(path[l-3]==‘L’ && path[l-1]==‘S’ && shortdone==0)
{
l-=3;
path[l]=‘R’;
shortdone=1;
}
if(path[l-3]==‘R’ && path[l-1]==‘L’ && shortdone==0)
{
l-=3;
path[l]=‘B’;
shortdone=1;
}

if(path[l-3]==‘S’ && path[l-1]==‘L’ && shortdone==0)

{
l-=3;
path[l]=‘R’;
shortdone=1;
}

if(path[l-3]==‘S’ && path[l-1]==‘S’ && shortdone==0)

{
l-=3;
path[l]=‘B’;
shortdone=1;
}

if(path[l-3]==‘L’ && path[l-1]==‘L’ && shortdone==0)

{
l-=3;
path[l]=‘S’;
shortdone=1;
}

}
// FINAL PATH
void replay()
{

if(stage==1)

{

for(l=0; path[l]<=’\0’; l++)

{

if(path[l]==‘S’)

{

digitalWrite(13, HIGH );
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);

}
if(path[l]==‘L’)

{

digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);

}
if(path[l]==‘R’)

{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
}
else
{
digitalWrite(8, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
}
}
}
}
PLZ HELP ME OUT WITH THIS CODE AND FIND ANY BUG. ASAP

mazernd.ino (3.92 KB)

Well for a start use the code tag (not quote) edit your first post and insert the tag before and after the code and get rid of the second duplicate post which is ugly.

Then ask a question. What help do you need? What works, what does not?what's the behavior you get versus what you think you should get?

You're kidding! First you post your code without code tags, then you quote that great big, long post without adding anything, obviously just to bump your thread up (after a whole 7 minutes with no answer).

My standard reply:-
You really should have read How to use this forum before posting. (Especially item #7.)

ie Your code and any error messages should always be placed between [code]code tags[/code]. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

(And you might as well delete that second post since it serves no purpose whatsoever.)

J-M-L just said much the same, but now that I've typed this.....

First you post your code without code tags, then you quote that great big, long post without adding anything, obviously just to bump yout thread up

It's OK, I deleted that one.

PLZ HELP ME OUT WITH THIS CODE ASAP

Give your pins some names.

It now running properly my hardware arrangement is ok

Good; problem solved.

AWOL:
It's OK, I deleted that one.

Thanks for that. :slight_smile:

Put your code in code tags.

Use meaningful constants for your in names.

Use meaningful variable names for the values you read off the pins.

Don't compare the return value of digitalRead to 0 or 1, compare it to HIGH or LOW.

Don't use [nobbc]path[l-2][/nobbc] when I is initialised to zero.

hey paul thanx for suggestion.

I Change the code as per ur suggestion but my bot is not even move.

So first you said:-

It now running properly my hardware arrangement is ok

Then later you go back and change it to:-

It's not running properly my hardware arrangement is ok and this is my code.
When i am storing character in an array my bot doesn't follow line and sometimes it hangs. I think there is a bug in the code

You shouldn't completely change your opening post like that. It makes a mockery of the responses of those who replied.

Too many people go back and butcher their opening posts here. It's a PITA.
(And I see that you've totally ignored all requests to edit and place code tags around your code. It's mandatory on these forums, you know.)