I have seen so many requests for code for using various sensors that I decided to post what I have written. This is code for the IRPD (Infra Red Proximity Detector) sensor that I wrote for W.A.L.T.E.R. It is based loosely on some code I found here. I need to add a few more comments. Feel free to ask any questions you may have about this code.
[code]’ IRPD Configuration
’
rights con p12 ’ Change pin number to pin you are using for your right led Voilet wire
lefts con p13 ’ Change pin number to pin you are using for your left led Blue wire
sensor var in14 ’ Change pin number to pin you are using for your sensor Yellow wire
’ For obstacle detection checks, to make the code easier to understand
IRPD_Left con 1
IRPD_Right con 2
’
’ IRPD Variables
’
templ var bit ’ Left sensor status
tempr var bit ’ Right sensor status
lastdet var nib ’ Last detection status
IRPD_Status var nib ’ 0 = Nothing detected, 1 = Left Side Hit, 2 = Right Side Hit, 3 = Center Hit
scanning var bit ’ 1 = Scanning behaviors, 0 = Non scanning behaviors
scancnt var byte ’ Scan counter
scandetect var nib ’ Scan Detection control - we ignore detected value in the Scan subroutine because
’ we only care if something was detected, not what or where it was detected.
scanreps var byte ’ Number of repetitons per scan
Check_IRPD
’ Loop modifications suggested by Jim Frye of Lynxmotion (http://www.lynxmotion.com)
’
’ Check the Left IR Emitter
for loop = 1 to 4
high lefts
pause 1
low lefts
pause 1
next
high lefts
pause 1
templ = sensor
low lefts
pause 1
' Check the Right IR Emitter
for loop = 1 to 4
high rights
pause 1
low rights
pause 1
next
high rights
pause 1
tempr = sensor
low rights
pause 1
' Set Obstacle Detection Flag
IRPD_Status = 0
if templ = 0 then
IRPD_Status = IRPD_Status + 1
if makenoise then
sound speaker,[dur\note1, dur\note2, dur\note3, dur\note4]
endif
endif
if tempr = 0 then
IRPD_Status = IRPD_Status + 2
if makenoise then
sound speaker,[dur\note4, dur\note3, dur\note2, dur\note1]
endif
endif
lastdet = IRPD_Status
return[/code]
8-Dale