program firefight6
Dim x as byte
Dim Duration as Integer
Dim SCenterAve as word
Dim SRightAve as word
Dim SLeftAve as word
Dim flamecenter as word
Dim flameright as word
Dim flameleft as word
'*************************
Sub Procedure Forward
PortC = %10100101 'Bits 2 and 5 are Enable
End Sub
'*************************
Sub Procedure Reverse
PortC = %01100110
Delay_ms(50)
PortC = 0
Delay_ms(5)
End Sub
'*************************
Sub Procedure T_Right
PortC = %10100110
Delay_ms(50)
PortC = 0
Delay_ms(5)
End Sub
'*************************
Sub Procedure T_Left
PortC = %01100101
Delay_ms(50)
PortC = 0
Delay_ms(5)
End Sub
'*************************
Sub Procedure Halt
PortC = %00000000
End Sub
'*************************
Sub Procedure Sort
If SRightAve > SCenterAve Then
Duration = 8
While Duration > 1
T_left
Duration = Duration - 1
Wend
End If
If SLeftAve > SCenterAve Then
Duration = 8
While Duration > 1
T_Right
Duration = Duration - 1
Wend
End If
If SCenterAve > 250 Then
Duration = 12
While Duration > 1
T_left
Duration = Duration - 1
Wend
End If
End Sub
'*************************
Sub Procedure Sortflame
If flameright < flamecenter then
Duration = 11
While Duration > 1
T_Right
Duration = Duration - 1
Wend
End If
If flameleft < flamecenter then
Duration = 11
While Duration > 1
T_Left
Duration = Duration - 1
Wend
End If
If flamecenter < 100 then
Halt
x = 60
Do
SetBit(PortB,1)
Delay_us(800)
ClearBit(PortB,1)
Delay_ms(20)
x = x - 1
loop Until x = 0
x = 42
Do
SetBit(PortB,1)
Delay_us(2500)
ClearBit(PortB,1)
Delay_ms(20)
x = x - 1
loop Until x = 0
Delay_ms(1000)
End If
End Sub
'*************************
Sub Procedure Center
x = 12
Do
SetBit(PortB,0)
Delay_us(1500)
ClearBit(PortB,0)
Delay_ms(20)
x = x - 1
Loop Until x = 0
SCenterAve = Adc_Read(1)
flamecenter = Adc_Read(0)
End Sub
'********************
Sub Procedure BlindCenter
x = 12
Do
SetBit(PortB,0)
Delay_us(1500)
ClearBit(PortB,0)
Delay_ms(20)
x = x - 1
Loop Until x = 0
End Sub
'********************
Sub Procedure FarRight
x = 20
Do
SetBit(PortB,0)
Delay_us(2000)
ClearBit(PortB,0)
Delay_ms(20)
x = x - 1
Loop Until x = 0
SRightAve = Adc_Read(1)
flameright = Adc_Read(0)
End Sub
'********************
Sub Procedure FarLeft
x = 17
Do
SetBit(PortB,0)
Delay_us(800)
ClearBit(PortB,0)
Delay_ms(20)
x = x - 1
Loop Until x = 0
SLeftAve = Adc_Read(1)
flameleft = Adc_Read(0)
End Sub
'********************
Sub Procedure IRScan
iF FlameCenter > 300 Then
If SCenterAve > 300 then
Sort
end if
If SRightAve > 300 then
Sort
end if
If SLeftAve > 300 then
Sort
end if
End IF
End Sub
'********************
Sub Procedure FlameScan
If flamecenter < 600 then
Sortflame
end if
If flameright < 700 then
Sortflame
end if
If flameleft < 700 then
Sortflame
end if
End Sub
'********************
main:
trisb = %00000000
trisa = %00011111
trisc = %00000000
ADCON1 = %10000000
'sound_init(PortC,3)
PortB = 0
FarRight
Center
FarLeft
While True
FarRight
Center
FarLeft
Flamescan
IRScan
Forward
BlindCenter
Wend
End.