CODE:
Below are samples of code necessary for certain functions of the bot that others may have trouble with.
For complete code, please see the "DOWNLOADS" page.


Building Packet to be Sent(bot):
Build_Packet:
GOSUB Clear_Buffer
header = STX ' build TX packet
cmd = LightLeds
data1 = 3
GOSUB Make_CheckSum
GOSUB Show_TX_Packet
lastCmd = cmd

'This allows a packet to bebuild for sending

'set data value to 3 for sending out, this value can be changed if necessary to any byte

Sending the Packet Built(bot):
TX_Packet:
GOSUB IR_Buddy_Reset
HIGH TxLED
SEROUT IRbSIO, IRbBaud, [IRbDataTx, IRbMod, STR buffer\8]
PAUSE 5


'Resets the buddy

'sends out the packet

' time allowed for reciever to pick up data

Recieve the Packet Back fromTarget(bot):

RX_Packet:
GOSUB IR_Buddy_Reset
SEROUT IRbSIO, IRbBaud, [IRbDataRx]
SERIN IRbSIO, IRbBaud, 1000, TO_Error, [STR buffer\8]
IF buffer(2) = 3 THEN DEBUG "RECIEVED!"
IF buffer(2) = 3 AND radarcount=0 THEN sonic
IF buffer(2) = 3 THEN turn

IF righton=0 THEN GOSUB radarright
IF righton=1 THEN GOSUB radarleft



' prep for 8-byte RX


'Debugs so that we know that it is recieving the correct value
'If the value is recieved and IR buddy has not rotated, then it will go to be adjusted to target
'if it recieves the right value, it will go to be adjusted
'This adjusts it so that it will keep searching if target is not found



Adjusting the Turning Once Target is Found(bot):
turn:

radarhold=radarcount
IF radarcount=0 THEN adjust 'the target
FOR counter=0 TO radarhold
FOR counter1=1 TO 6
PULSOUT 12, 1000
NEXT
radarcount=0

PAUSE 20
FOR counter1=1 TO 4
PULSOUT 13, 823
PULSOUT 14, 805
PAUSE 20
NEXT
radarcount=0

NEXT
radarhold=0
righton=0
GOTO sonic

'turns the robot in accordance with the number of
'times the "radar" moved and locks the robot on with
'Turning IR Buddy






'Turning entire bot(wheels)









'goes to be adjusted forward or backwards
Using the Ultrasonic device(bot):
sonic:
GOSUB Get_Sonar ' take sonar reading
FREQOUT 8, 1, 58500
cm = rawDist / ToCm
DEBUG CRSRXY, 15, 3, DEC cm, CLREOL
GOTO adjust
' -----[ Subroutines ]-----------------------------------------------------
adjust:
IF cm > 10 THEN forward
IF cm < 10 THEN backward
IF cm = 10 THEN stop1
Get_Sonar:
DEBUG "Sonar Got"
rawDist = 0
FOR samples = 1 TO 5
PULSOUT Trigger, Trig10
#SELECT $STamp
#CASE BS2, BS2E
RCTIME Echo, 1, pWidth
#CASE #ELSE
PULSIN Echo, 1, pWidth
#ENDSELECT
rawDist = rawDist + (pWidth / 5)
PAUSE 10
NEXT
RETURN
'This inputs and adjusts the bot forwards or backward

' convert to centimeters





'This sends the program to be adjusted 10cm


'needed to obtain the sonar distance
' clear measurement
' take five samples
' 10 uS trigger pulse


' measure pulse

' measure pulse

' simple digital filter
' minimum period between




Final Function,Launching of Projectile(bot)
stop1:
FOR counter =1 TO 300
PULSOUT 15, 800
NEXT
STOP

'Turns the servo to hit switch to launch projectile
Recieve Data from controller(target):
RX_Packet:
SERIN IRbSIO, IRbBaud, 2000, TO_Error, [STR buffer\8]
errorLevel = %0000

'recieves data
Checks the Data Recieved(target):
Check_RX_Packet:
IF (header = STX) THEN
rxChkSum = chkSum
GOSUB Make_CheckSum
IF (rxChkSum <> chkSum) THEN
errorLevel = errorLevel | ErrChkSum
ENDIF
ELSE
errorLevel = errorLevel | ErrHeader
ENDIF


' save rx checksum
' calc checksum of data sent
' checks to see data was sent correctly

Makes a Packet to be Sent Based on Data Recieved(target)

Make_Packet:
IF (errorLevel = 0) THEN
header = ACK
ELSE
header = NAK
data1 = errorLevel
ENDIF
GOSUB Make_CheckSum

Make_CheckSum:
chkSum = 0
FOR idx = 0 TO 6
chkSum = chkSum + buffer(idx)
NEXT
RETURN






'assures no errors



' checksum of bytes 0 to 6




Sends Back Data(target):


TX_Packet:
GOSUB IR_Buddy_Reset
SEROUT IRbSIO, IRbBaud, [IRbDataTx, IRbMod, STR buffer\8]
PAUSE 5




'sends data