Home Chapter 9 BS2 Programming the project continued

Site Search

GTranslate

Chinese (Simplified) French German Italian Portuguese Russian Spanish
BS2 Programming the project continued


In the next program, you will be counting the number of times the sensor sees infrared quantities. This will be a valuable program as it will allow you to program different events that are the result of the number of sensor hits that the PIR senses.

If the sensor hits one time it can do one thing but if the sensor hits 5 or 7 times, etc. than different pre-programmed behaviors will manifest themselves.

Enter the program below

' {$STAMP BS2}
' {$PBASIC 2.5}

Total VAR Word 'create a variable named "total".

X VAR Word 'create a variable named "x".

INPUT 0 'make pin 0 an input to receive PIR data.

OUTPUT 4 'make pin 4 an output to be able to activate thee solid state relay

X=1 ‘sets the beginning value of x to equal 1.

Look4data: ‘A label called look4date.

LOW 4 'Make sure relay is turned off.

IF (IN0 = 1) THEN add 'if PIR senses movement then goto "add" subroutine

PAUSE 200 ‘Pause for 200 microseconds.

GOTO look4data ‘Go back to the subroutine called look4data.

Add: 'This subroutine uses a FOR command to count the
‘number of times the PIR is triggered.

FOR total = 1 TO x 'Creates a loop to flash once for every previous cycle

HIGH 4 'Turn relay on or high.

PAUSE 200/x 'Dividing the time by the number of cycles, helps keep
‘the overall period of flashing the same.

LOW 4 ‘Turn solid state relay off.

PAUSE 700/x ‘Pauses the program determined by the increasing value of x.

NEXT ‘Advances the FOR NEXT loop.

IF (x=5) THEN take_5 'When the count reaches five goto a new subroutine to
‘perform an action and reset the count

X=x+1 'Increase count by one

GOTO look4data

Take_5:

X=1 'Reset count to 1 to be used for number of rapid flashes

FOR x=1 TO 20 'Creates a series of rapid flashes

HIGH 4 ‘SSR high or on.

PAUSE 10 ‘Pause for 10 milliseconds

LOW 4 ‘SSR low or off.

PAUSE 20 ‘Pause for 20 milliseconds.

NEXT ‘Advance the FOR NEXT loop.

X=1 'resets count to 1 before starting to count at beginning of
‘program

GOTO look4data