You are on page 1of 17

#include "coord.

h"

//struct coordinate{byte x; byte y;};

//given a matrix, set the turnstatus of each cell appropriately for the function
fill

unsigned char data[70]; //data recieved from the serial communication

unsigned char lengthofpath=0;

boolean serialcomm=true;

boolean recievedata=true;

boolean sendpath=false;

coord follow[70];

int turnstatus[70];

unsigned char pathbyte[70];

unsigned char okflag1=0x02;

unsigned char okflag2=0x02;

unsigned char i=0;

unsigned char nooftimes=0; //no. of times data has been recieved from them

unsigned char points[6][6];

boolean checksum1=false;

boolean checksum2=false;

unsigned char noofcheckpoints;

unsigned char noofdangerpoints;

unsigned int dummy=0;

unsigned char hash;

unsigned char ORGID=197; // so that I see it as (!)

void processdata();

void processfollow();

void getturnstatus(int turnstatus[70],coord follow[70],int lengthofpath);


void dayaa(int turnstatus[70]);

int a1;

int a2;

int a3;

int a4;

int a0;

int junc=0;

int th=600;

int td1=165;

void getval()

a1=analogRead(1);

a2=analogRead(2);

a3=analogRead(3);

a4=analogRead(4);

a0=analogRead(0);

void turnleft() //turn left at junction

analogWrite(9,255);

analogWrite(10, LOW);

analogWrite(5,LOW);

digitalWrite(6,255);
delay(td1);

while(0==0)

getval();

analogWrite(9,50);

analogWrite(6,50);

digitalWrite(5,LOW);

digitalWrite(10,LOW);

if(a0<th&&a4<th&&a1<th&&a3<th&&a2>th)break;

void turnright() //turn right at junction

digitalWrite(9,LOW);

analogWrite(10, 255);

analogWrite(5,255);

digitalWrite(6,LOW);

delay(td1);

while(0==0)

getval();

analogWrite(10,50);
analogWrite(5,50);

digitalWrite(6,LOW);

digitalWrite(9,LOW);

if(a0<th&&a4<th&&a1<th&&a3<th&&a2>th)break;

void halt() //bring the bot to a complete halt

digitalWrite(10, LOW);

digitalWrite(6, LOW);

digitalWrite(9, LOW);

digitalWrite(5, LOW);

void setup()

Serial.begin(9600);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

pinMode(10,OUTPUT);

pinMode(11,OUTPUT);
}

void loop()

digitalWrite(11,HIGH);

while((serialcomm==true)&&(Serial.available()>0)&&(recievedata==true))

Serial.println("nancy");

if(dummy!=0) // not recieved OK/fail but next data

data[0]=dummy;

dummy=0;

i=1;

else if(i==0)

hash=Serial.read();

while(Serial.available())

data[i]=Serial.read();

i++;

if(i>(data[0]-1))

digitalWrite(11,LOW);

recievedata=false;
nooftimes++;

checksum1=true;

delay(2000);

while((serialcomm==true)&&(recievedata==false)&&(sendpath==false)&&
(checksum1==true))

unsigned char sum=0;

for(i=0;i<data[0];i++)

sum+=data[i];

i=0;

unsigned char sum1[4];

sum1[0]=35;

sum1[1]=2;

sum1[2]=ORGID;

sum1[3]=sum;

digitalWrite(11,HIGH);

Serial.print(sum1[0]);

Serial.print(sum1[1]);

Serial.print(sum1[2]);

Serial.print(sum1[3]);

while(Serial.available()<3)

{}

if(nooftimes>3)

processdata(); // makes an array points according to the data recieved


lengthofpath=path(noofcheckpoints+1, points, follow);

processfollow();

sendpath=true;

checksum1=false;

recievedata=false;

else if(Serial.available()>2)

hash=Serial.read();

dummy=Serial.read();

if(dummy==0x02)

okflag1=dummy;

okflag2=Serial.read();

if((okflag1==0x02)&&(okflag2==0x1))//&&(nooftimes<3)) //received
OK //40 =( 41 = )

digitalWrite(11,LOW);

delay(1000);

processdata(); // makes an array points according to the data recieved

lengthofpath=path(noofcheckpoints+1, points, follow);

processfollow();

sendpath=true;

checksum1=false;

recievedata=false;

else
{ recievedata=true;

dummy=0;

checksum1=false;

else //case when has not received ok/fail, but next data, when dummy!
=0x02

recievedata=true;

checksum1=false;

while((serialcomm==true)&&(sendpath==true))

unsigned char sum=lengthofpath;

char path[lengthofpath+2];

path[0]=35;

path[1]=lengthofpath;

path[2]=ORGID;

Serial.print(lengthofpath);

for(i=1;i<lengthofpath;i++)

sum+=pathbyte[i];

path[i+2]=pathbyte[i];

}
Serial.print(path);

digitalWrite(11,HIGH);

dummy=0;

while(Serial.available()<3)

{}

hash=Serial.read();

dummy=Serial.read();

dummy=Serial.read();

digitalWrite(11,LOW);

delay(2000);

digitalWrite(11,HIGH);

delay(2000);

if(dummy==sum)

unsigned char y=35;

Serial.print(y);

y=2;

Serial.print(y);

y=ORGID;

Serial.print(y);

y=1;

Serial.print(y);

sendpath=false;

checksum2=true;

else
{

unsigned char y=35;

Serial.print(y);

y=2;

Serial.print(y);

y=ORGID;

Serial.print(y);

y=1;

Serial.print(y);

digitalWrite(11,LOW);

while((serialcomm==true)&&(sendpath==false)&&(recievedata==false)&&
(checksum2==true))

while(Serial.available()<3)

{ }

hash=Serial.read();

okflag1=Serial.read();

okflag2=Serial.read();

delay(2000);

digitalWrite(11,HIGH);

if((okflag1==0x02)&&(okflag2==0x13)) // go

serialcomm=false;

if(serialcomm==false)
{

// Serial.print("#g");

// unsigned char y=ORGID;

// Serial.print(y);

// Serial.print("oing"); // sent a smaller packet than the length ... just for
the aesthetics!

getturnstatus(turnstatus,follow,lengthofpath);

digitalWrite(11,LOW);

while(1)

dayaa(turnstatus);

void getturnstatus(int turnstatus[70],coord follow[70],int lengthofpath)

int k;

int left=0;

int right=2;

int straight=1;

int back=3;

int end=4;

int changex;

int changey;

turnstatus[lengthofpath-1]=end;
for(k=1;k<lengthofpath;k++)

if(follow[k-1].x==follow[k].x) //coming in y direction

if(follow[k+1].x==follow[k].x) //going in y direction

if(follow[k+1].y==follow[k-1].y) // going back

turnstatus[k-1]=back;

else

turnstatus[k-1]=straight;

else

changey=follow[k].y-follow[k-1].y;

changex=follow[k+1].x-follow[k].x;

if(changex==changey)

turnstatus[k-1]=right;

else

turnstatus[k-1]=left;

else if(follow[k-1].y==follow[k].y) //coming in x direction

if(follow[k+1].y==follow[k].y) //going in x direction

if(follow[k+1].x==follow[k-1].x) // going back


turnstatus[k-1]=back;

else

turnstatus[k-1]=straight;

else

changey=follow[k+1].y-follow[k].y; //********CHANGE HERE**********

changex=follow[k].x-follow[k-1].x;

if(changex==changey)

turnstatus[k-1]=left;

else

turnstatus[k-1]=right;

if (k==1) //********NOTE THIS*************

turnstatus[k-1]=left;

void processdata()

unsigned char m;

unsigned char n;

for(m=0;m<6;m++)
for(n=0;n<6;n++)

points[m][n]=40;

points[0][0]=0;

noofcheckpoints=data[2];

m=byte(data[1]/16); // xy is destination coordinates

n=data[1]%16;

points[m][n]=101+noofcheckpoints;

noofdangerpoints=data[(data[2]+3)];

for(int i=0;i<noofcheckpoints;i++)

n=data[i+3]%16;

m=byte(data[i+3]/16);

points[m][n]=101+i;

for(i=0;i<noofdangerpoints;i++)

n=data[(i+data[2]+4)]%16;

m=byte(data[(i+data[2]+4)]/16);

points[m][n]=201;

}
void processfollow()

for(int i=0;i<lengthofpath;i++)

pathbyte[i]=(follow[i+1].x*16)+(follow[i+1].y);

void dayaa(int turnstatus[70])

digitalWrite(9, LOW);

digitalWrite(5, LOW);

digitalWrite(11,LOW);

getval();

if(a1>th||a0>th)

analogWrite(6, 110);

digitalWrite(10, HIGH);

if(a3>th||a4>th)

analogWrite(10, 110);

digitalWrite(6, HIGH);

if(a0>th&&a4>th)
{

digitalWrite(10, LOW);

digitalWrite(6, LOW);

digitalWrite(11, HIGH);

if (turnstatus[junc]==2)

turnright();

if (turnstatus[junc]==0)

turnleft();

if (turnstatus[junc]==1)

if (turnstatus[junc]==3)

turnright();

halt();

delay(200);

turnright();

if (turnstatus[junc]==4)

{
halt();

delay(10000);

junc++;

if(a3<th&&a1<th&&a0<th&&a4<th)

digitalWrite(10, HIGH);

digitalWrite(6, HIGH);

You might also like