Customize the Final Program

Custom Characters and the Final Program

Figure 4 - Alarm LCD Display

This example alarm makes use of the custom characters that are set aside for the Parallax Serial LCD, specifically to display a Jolly Roger – or skull and crossbones.  Smart Sensors and Applications Chapter 1, Activity #4 demonstrates how to define your own custom characters to display as you please.  If you don’t like the Jolly Roger, replace it with whatever you want!

Once you’ve decided on a final display for your alarm, it’s time to run the final program.  Before programming the alarm, there are two parts of the program that you will have to replace:

  • The maxDist constant, which is the echo time of the Ping))) sensor when placed unobstructed in the area you want to monitor; update it with the value found from running TestAlarmCircuit.bs2 in that place.
  • Jolly Roger custom characters: replace this section with the custom characters you want to use.
' -----[ Title ]-----------------------------------------------------------
' DoorAlarm.bs2
' Monitors a doorway until an object is in the way, then an alarm sounds,
' and the number of triggers are recorded and displayed to the LCD.

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

' -----[ Variables ]-------------------------------------------------------

time         VAR   Word            ' Round trip echo time
intruderCnt  VAR   Byte            ' Count of alarm triggers
counter      VAR   Word            ' Counter for FOR...NEXT loop

' -----[ Constants ]-------------------------------------------------------

maxDist      CON   2000            ' Maximum distance for alarm trigger

' -----[ Jolly Roger Custom Characters ]-----------------------------------

SEROUT 11, 84, [248, %00000, %11000, %11000, %01100,       ' Top left
                %01110, %11011, %11001, %00000]            ' crossbone

SEROUT 11, 84, [249, %00000, %11001, %11011, %01110,       ' Bottom left
                %01100, %11000, %11000, %00000]            ' crossbone

SEROUT 11, 84, [250, %00000, %01110, %11111, %11111,       ' Top half
                %10101, %10101, %11111, %01110]            ' of skull

SEROUT 11, 84, [251, %01110, %11111, %11111, %10001,       ' Bottom half
               %10001, %11111, %01110, %00000]             ' of skull

SEROUT 11, 84, [252, %00000, %00011, %00011, %00110,       ' Top right
                %01110, %11011, %10011, %00000]            ' crossbone

SEROUT 11, 84, [253, %00000, %10011, %11011, %01110,       ' Bottom right
                %00110, %00011, %00011, %00000]            ' crossbone

' -----[ Initialization ]--------------------------------------------------

SEROUT 11, 84, [22,12]             ' Initialize LCD
PAUSE 5

intruderCnt = 0                    ' Reset intruder count to 0

' -----[ Main Routine ]----------------------------------------------------

DO
  PULSOUT 15, 5                              ' Send pulse
  PULSIN  15, 1, time                        ' Read echo time

  IF (time < maxDist) THEN                   ' If alarm is triggered...
    intruderCnt = intruderCnt + 1

    SEROUT 11, 84, [128, 0, 129, 2, 130, 4,  ' Display Jolly Roger
                    148, 1, 149, 3, 150, 5]
    SEROUT 11, 84, [132, DEC intruderCnt,    ' Display number of alarm
                    " intruders "]           ' triggers on the LCD
    SEROUT 11, 84, [153, "detected!! "]

    FOR counter = 0 TO 3
      FREQOUT 14, 500, 3800                  ' Play alarm tone
      PAUSE 100
      HIGH 13                                ' Flash LEDs
      HIGH 12
      FREQOUT 14, 500, 2700                  ' Play lower tone for variety
      PAUSE 100
      LOW 13                                 ' Flash LEDs
      LOW 12
    NEXT

  ELSEIF (intruderCnt = 0) THEN
    SEROUT 11, 84, [128, 0, 129, 2, 130, 4,  ' Display Jolly Roger
                    148, 1, 149, 3, 150, 5]
    SEROUT 11, 84, [133, "Alarm",
                    153, "Armed!"]
    SEROUT 11, 84, [141, 0, 142, 2, 143, 4,  ' Display Jolly Roger
                    161, 1, 162, 3, 163, 5]
  ENDIF

  PAUSE 100
LOOP
  • After you’ve set the alarm, place it in the area you want to monitor and make sure it doesn’t activate when nothing is in its way.  If it does, try decreasing the maxDist constant until the alarm only activates as someone walks by.

 

How it Works

First, the program stores the echo time measurement from the Ping))) sensor into a variable named time.

PULSOUT 15, 5
PULSIN  15, 1, time

That value is then compared to the maxDist constant defined at the beginning of the program.  If the echo time is less than that value, it displays the total number of times the alarm was triggered and sounds the alarm.

IF (time < maxDist) THEN
  intruderCnt = intruderCnt + 1

  SEROUT 11, 84, [128, 0, 129, 2, 130, 4,
                  148, 1, 149, 3, 150, 5]
  SEROUT 11, 84, [132, DEC intruderCnt,
                  " intruders "]
  SEROUT 11, 84, [153, "detected!! "]

  FOR counter = 0 TO 3
    FREQOUT 14, 500, 3800
    PAUSE 100
    HIGH 13
    HIGH 12
    FREQOUT 14, 500, 2700
    PAUSE 100
    LOW 13
    LOW 12
  NEXT

If no intruders have been detected, the LCD displays that the alarm is armed to deter people from entering the room.

ELSEIF (intruderCnt = 0) THEN
  SEROUT 11, 84, [128, 0, 129, 2, 130, 4,
                  148, 1, 149, 3, 150, 5]
  SEROUT 11, 84, [133, "Alarm",
                  153, "Armed!"]
  SEROUT 11, 84, [141, 0, 142, 2, 143, 4,
                  161, 1, 162, 3, 163, 5]

Custom Characters at a Glance
Let’s take a look at the SEROUT statement needed to display the Jolly Roger:
SEROUT 11, 84, [128, 0, 129, 2, 130, 4, 148, 1, 149, 3, 150, 5]
Remember from Smart Sensors and Applications that the numbers 0–5 all correspond to the custom characters defined at the beginning of the program.  The numbers 128–130 and 148–150 tell the LCD where to display those characters.  You can find a full listing of both decimal and hexadecimal commands for the Parallax Serial LCD in Appendix B of the Smart Sensors and Applications PDF.

 

Get Creative!

This project gives you a simple application for this alarm.  However, there are several things to keep in mind when implementing this design.  Take a moment to consider each one and come up with your own solution!  It could be as simple as disconnecting parts or adding another extension cable to the Ping))) sensor so you can mount it away from the board.  In any case, each situation is different, and you’ll have to find the solution that best works for you!

  • If you aren’t home when the alarm goes off…
    • Problem: What if the alarm is triggered and the intruder finds the board and unplugs the battery? Is there a stealthier way to hide the alarm, or a way to modify the code to add a ‘Stealth Mode’?
  • If you have pets…
    • Problem: How will you make sure that a person is triggering the alarm and not your pet?
  • If a persistent intruder knows where the alarm is hidden…
    • Problem: What if you have a persistant intruder who knows the location of the alarm and constantly disconnects power to the board? Is there a way to write the number of intrusions to memory so you can recover it even if power has been disconnected? (Hint: Smart Sensors and Applications Chapter 6.)
  • If you’re a grammar nut…
    • Problem: You may notice that the word “intruders” is displayed even if only one intruder is detected. (The statement ‘1 intruders detected’ simply isn’t proper English.) Is there a way you can modify the code to do the following:
      • If 0 intruders are detected, the LCD displays:
        “No intruders detected!”
      • If 1 intruder is detected, the LCD displays:
        “1 intruder detected!”
      • If 2 or more intruders detected, the LCD displays:
        “# intruders detected!”