Coding Problem on Home Project
I have been building a system as a project and have run into an issue with the sensitivity of the PIR sensors that I am using. I have the regular parallax PIR sensors connected via servo ports to the board of education and LED lights that represent breaks in the sensors. Right now the issue I am having is that I have to create a couple of For loops so that the device will keep checking the responses from the sensors after one of the sensors is tripped which send the device back to passively checking for breaks after another sensor goes high or time ends. Unfortunately when one of these PIR sensors is tripped it shows something like 00000000111111110000000 which means that sometimes there is overflow and after the device goes back to passively checking the sensors it thinks that there is another trip and repeats the response creating a lot of false positives. I was wondering if anyone knew any coding technique where I could fix this in some way so that either the sensors were forced back to a low state from high (which I doubt could happen) or another averaging trick or something like that. I'm not really sure how to approach this problem so please let me know. I have attached the code below as well if that is any help.
CODE:
CODE:
' {$STAMP BS2e}
' {$PBASIC 2.5}
' -----[ I/O Definitions ]-------------------------------------------------
OUTPUT 0
OUTPUT 1
OUTPUT 3
LED PIN 3
LED1 PIN 4
TX PIN 8 ' Transmit Data --> 27937.4 (RXD)
RTS PIN 9 ' Request To Send --> 27937.6 (CTS)
RX PIN 10 ' Receive Data <-- 27937.5 (TXD)
CTS PIN 11 ' Clear To Send <-- 27937.2 (RTS)
' -----[ Constants ]-------------------------------------------------------
Baud CON 84 ' Serial Baud Rate 9600 bps (BS2)
NumSamples CON 1 ' Number Of Samples To Log
' -----[ Variables ]-------------------------------------------------------
buffer VAR Byte(15) ' Input Buffer
index VAR Byte ' Index Variable
ioByte VAR Byte ' Input/Output Storage
counter VAR Word ' Counter Variable
result VAR Word ' Sensor RCTIME Result
work VAR Word ' Work Variable
flag VAR Bit ' Event Status Flag
reps VAR Word
' -----[ Initialization ]--------------------------------------------------
DEBUG CLS, "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..."
PAUSE 200 ' Allow Time To Settle
HIGH TX ' Initialize Transmit Line
LOW RTS ' Take Vinculum Out Of Reset
PAUSE 600 ' Allow Time To Settle
DEBUG "Done!", CR, "Synchronizing..."
DO
SEROUT TX\CTS, Baud, ["E", CR] ' Sync Command Character
GOSUB Get_Data ' Get Response
PAUSE 250
LOOP UNTIL ioByte = $0D ' Wait For Carriage Return
DO
SEROUT TX\CTS, Baud, ["e", CR] ' Sync Command Character
GOSUB Get_Data ' Get Response
PAUSE 250
LOOP UNTIL ioByte = $0D ' Wait For Carriage Return
' -----[ Initialization for Disk Drive]--------------------------------------------------
DEBUG CLS, "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..."
PAUSE 200 ' Allow Time To Settle
HIGH TX ' Initialize Transmit Line
LOW RTS ' Take Vinculum Out Of Reset
PAUSE 600 ' Allow Time To Settle
DEBUG "Done!", CR, "Synchronizing..."
DO
SEROUT TX\CTS, Baud, ["E", CR] ' Sync Command Character
GOSUB Get_Data ' Get Response
PAUSE 250
LOOP UNTIL ioByte = $0D ' Wait For Carriage Return
DO
SEROUT TX\CTS, Baud, ["e", CR] ' Sync Command Character
GOSUB Get_Data ' Get Response
PAUSE 250
LOOP UNTIL ioByte = $0D ' Wait For Carriage Return
' -----[ Program Code ]----------------------------------------------------
Main:
DEBUG "Done", CR, "Switching to Short Command Mode..."
SEROUT TX\CTS, Baud, ["SCS", CR] ' Switch To Short Command Mode
GOSUB Get_Data ' Purge Receive Buffer
DEBUG "Done!", CR, "Waiting for Memory Stick..."
Check_Drive:
DO
SEROUT TX\CTS, Baud, [CR] ' Prompt Device For Status
GOSUB Get_Data ' Purge Receive Buffer
IF buffer(0) = ">" THEN ' Check For Ready Prompt
EXIT ' If Ready Then Exit Loop
ELSEIF buffer(0) = "N" AND buffer(1) = "D" THEN
DEBUG "." ' Device Ready But No Memory Stick
ELSEIF buffer(0) = "D" AND buffer(1) = "D" AND flag = 0 THEN
DEBUG "Connected!", CR, "Accessing..."
flag = 1 ' Memory Stick Ready
ELSE
DEBUG "."
ENDIF
PAUSE 250 ' Command Retry Delay
LOOP
DEBUG "Ready!", CR
'-------------------------Initialize Sensor-------------------------------
Initialize_Sensor:
FOR reps = 0 TO 120
DEBUG DEC reps
PAUSE 1000
NEXT
GOTO Sensor
'------------------------------Start Sensor----------------------------
Sensor:
PAUSE 1000
LOW LED
HIGH LED1
DO
DEBUG "Handwash1:"
DEBUG BIN IN15, CR, LF
DEBUG "Door:"
DEBUG BIN IN12, CR, LF
DEBUG "Handwash2:"
DEBUG BIN IN13, CR, LF
PAUSE 500
IF IN15 = 1 THEN Flash 'check if any are tripped
IF IN12 = 1 THEN Flash
IF IN13 = 1 THEN Trip
LOOP
Flash:
HIGH LED
LOW LED1
FOR reps = 0 TO 50 'keep checking for responses
IF IN13 = 1 THEN 'check for person walking through door
SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
GOSUB Get_Data ' Purge Receive Buffer
SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D, CR,
DEC5 0, ",", DEC5 1, CR, LF, CR]
PAUSE 500 ' Write walked through door after handwash used = 1
' Close File (MUST CLOSE!)
SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
GOTO Sensor
ENDIF
IF IN15 = 1 THEN
GOTO Flash
ENDIF
IF IN12 = 1 THEN
GOTO Flash
ENDIF
PAUSE 50
NEXT
SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
GOSUB Get_Data ' Purge Receive Buffer
SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D, CR,
DEC5 0, ",", DEC5 0, CR, LF, CR]
PAUSE 500 ' Write Handwash used without going through door = 0
' Close File (MUST CLOSE!)
SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
GOSUB Get_Data
GOTO Sensor
Trip:
FREQOUT 5, 1000, 2500
FOR reps = 0 TO 50
LOW LED1
PAUSE 100
HIGH LED1
PAUSE 100
LOW LED1
IF IN15 = 1 THEN 'check sensors for responses
SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
GOSUB Get_Data ' Purge Receive Buffer
SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D, CR,
DEC5 0, ",", DEC5 2, CR, LF, CR]
PAUSE 500 ' Write walked through door before handwash used = 2 (still compliance
' Close File (MUST CLOSE!)
SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
GOTO Sensor
ENDIF
IF IN12 = 1 THEN
SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
GOSUB Get_Data ' Purge Receive Buffer
SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D, CR,
DEC5 0, ",", DEC5 2, CR, LF, CR]
PAUSE 500 ' Write walked through door before handwash used = 2 (still compliance)
' Close File (MUST CLOSE!)
SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
GOTO Sensor
ENDIF
PAUSE 50
NEXT
SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
GOSUB Get_Data ' Purge Receive Buffer
SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D, CR,
DEC5 0, ",", DEC5 3, CR, LF, CR]
PAUSE 500 ' Write Handwash not used = 3 (noncompliance)
' Close File (MUST CLOSE!)
SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
GOSUB Get_Data
GOTO Sensor
' -----[ Subroutines ]-----------------------------------------------------
Get_Data:
index = 0 ' Reset Index Pointer
DO ' Receive Data
SERIN RX\RTS, Baud, 100, Timeout, [ioByte]
buffer(index) = ioByte ' Add Received Byte To Buffer
index = index + 1 ' Increment Index Pointer
IF index > 14 THEN Timeout ' Check For Overflow
LOOP
Timeout:
GOTO Sensor

Comments