You are on page 1of 4

//global variable(s) long int count = 0; //define count, please read below.

long int test=0; //define test char c='a'; //define c, a is use to prevent error, can dun put also

void measure (void) { unsigned int dur=0; //define dur, used in calculation of distance float cal =0; bit responseOK; responseOK =1; //define cal for calculate the distance //define //if sensor is ready, got response

if(receiverRdy()==1) //if receiver is ready, it will proceed otherwise it will continue on { c=getCh(); } if (responseOK) { putCh('\n'); if (cal <= 1000.0) { test++; mPin=0; if (test==15) this is to delay 3 sec { lPin=1; // laser on } //end of loop if (test==15) //to increase test every pulse, 200ms/pulse //motor stop moving when (enemy detected) //because tmr2DlySet(200); which is 200 ms /pulse, //sent each distance to C# software in a new line //get the char sent from C# software

if (count = 500000) below { if(c=='E') hardware {

// there isn t much practical use, refer

// E is sent from C# softeware to stop the

mPin=0; //motor off lPin=0; //laser off buzPin=0; //buzzer off }//end of loop if(c=='E') else { buzPin=1; count=0; //buzzer on //clear count

//end of loop else

}//end of loop if(count=500000) }//end of loop if (cal<=1000.0)

else // this alternatively means if (cal <= 1000.0) is false, cal>1000mm { if(c=='E') hardware { mPin=0; //motor off lPin=0; //laser off buzPin=0; //buzzer off // E is sent from C# softeware to stop the

}//end of loop if(c=='E')

else //this alternatively means if (c != 'E') != means not equal { mPin=1; //motor on lPin=0; //laser off test=0; }//end of loop else //clear test

}//end of loop outer else

dur=(int)cal; putDecU16LS(dur,5);putStr("mm"); /*this is the distance sent to C# softeware } // end of loop if (ResponseOK) void main (void) { Init_Device(); uartInit(1152); tmr2Init(2); // device initialization //serial port bautrate // Timer, refer to MPI notes

TMOD= (TMOD & 0xf0) | 0x01; //timer 0 mode 1 as 16-bit timer

//

basicTest(); mPin=0; lPin=0; //laser off buzPin=0; //busser off c=getCh(); if(c=='S') // get the char sent from C# software //if S is receive then start running

{ while(1) { count = count+1; measure(); } } } //add 1 till it reach 500000

*count: The used of variable count is quite redundant, as its purpose is only for the buzzer to sound each time count reach 500000

You might also like