Home Alarm System

Many homes and business have an alarm system. Do you know how they work? All alarm systems have a "brain"--a small computer--that processes instructions from the user and monitors the sensors attached to it. Most alarm systems use an ultrasonic (or infrared) sensor to detect the presense of intruders. Additional "contact" sensors are located on doors and windows. Special sensors can be added to detect other parts of the environment such as temperature, humidity, flood, glass breakage, etc.

All alarm systems have use "states" (or modes of operation)to determine what actions should be taken based on sensor inputs. For example, one "state" may be called: Disarmed. In its "disarmed" state, the alarm system usually does nothing. However, when in an "armed" state, the alarm system will sound the alarm if unauthorized intrusion is detected. Getting from one "state" to the next, remembering which state you are in, and providing an interface for the user all require some amount of programming. The experiment on this page shows how the NXT-G can be programmed to perform a basic home alarm system.

Figure 1
This is an expirement in the capabilities of NXT-G to implement multi-threaded (event driven) applications. The NXT-G software alows for multiple loops to execute at the same time at different sequences of the program "beam". When a new NXT-G program is started, notice how the starting point allows for three separate beams (see Figure 1).

Multi-Threaded Programs

Think of the program as a sentence or paragraph that is read by the computer. The words are read in order from left to right and from top to bottom. This can be thought of as a "thread" that winds its way through the code linking the different statements in a specific order of execution. The computer is able to keep track of where it is in the program through the use of an Instruction Pointer.

With Multi-Threaded Programs, the computer has multiple Instruction Pointers where each points to an instruction on a separate thread (program sequence).

In NXT-G, a multi-threaded program can be easily created by dragging several sequence beams from the starting block. Then drop an infinite loop onto each.

Home Alarm Systems

Home alarm systems monitor homes for unauthorized intrusion. When an intruder is detected, an alarm is sounded. The alarm system has several modes during its operation. These modes are: disarmed, armed, and sound the alarm. When the homeowner is at home, the alarm system is generally disarmed. When the homowner decides to leave the premises, they will begin the arming process. When the arming process has successfully completed, the system will then be armed while th ehomeowner is away. When the homeowner returns, they will then disarm the system.

Our Alarm System

Our alarm system is composed of two touch sensors, one ultrasonic (motion) sensor, and one color sensor. The first touch sensor is attached to the door and detects when the door is closed.

Sensors and their positions

  1. Touch Sensor #1: is attached to the door or door frame. This sensor is open (un-touched) when the door is open. The sensor is closed (touched; orange button pushed in) when the door is closed.
  2. Touch Sensor #2: is attached to the NXT Brick and serves as a button used by the homeowner to arm the system.
  3. Ultrasonic Sensor: should be positioned on a wall looking toward the door or interior where an intruder may walk past.
  4. Color Sensor: should be attached to the NXT Brick and serves as the key reader used by the homeowner to disarm the system.

Procedures for Using the System

Steps to Arm the System
  1. Close the Door. In order for the system to be armed, it must detect that the door sensor is working. If, for example, the door sensor was not working, the alarm system might not know when to begin the disarming process (more about this later).
  2. Push in the touch sensor attached to the NXT Brick. With both sensors now pushed in, the program will go into the Arming mode.
  3. Exit the room. You may now leave the room through the door--closing as you leave. It is important to close the door after you have left the room so that the Alarm System will monitor the door as well as the ultrasonic (motion) sensor.

Steps to Disarm the System
  1. Open the Door. At this point the program will detect that the door has been opened. It will then begin a series of beeps and start a countdown timer. You will have a limited time (usually 60 seconds) upon which you will need to disarm the system.
  2. Show the Colored Key Card to the Color Sensor. The key to disarming the system is a colored card that you will keep on yourself. When you enter the room, show the key to the NXT's color sensor.

How the Program is Constructed

This simple alarm system program uses three variables:
  1. Mode which stores a single number representing the current mode of the system.
  2. Door Status which stores the current status of the door (open or closed)
  3. Motion Detected stores a value (true or false) that indicates if motion has been detected by the ultrasonic (motion) detector.
The alarm system contains several infinite loops that monitor various conditions:
  1. Set the Door Status Variable Loop. This loop simply checks the status of the door and stores that information in the Door Status variable.
  2. Arm the System Loop. This loop checks to see if both touch sensors are pressed and that the current mode of the system is "disarmed". If both are pressed and the system is currently disarmed, then the system will prepare for arming.
  3. Disarming the System Loop. This loop checks the color sensor to see if the key has been presented. If it has, then the system will be disarmed and the NXT Brick will say: "OK".
  4. Detect Motion Loop. This loop monitors the ultrasonic (motion) sensor for any changes. If it detects that an object has changed position (moved) within a 1/10th of a second interval, then the Motion variable is updated to store the value representing "Motion Detected".
  5. Detect Intruder Loop. This loop will trigger the alarm if the system has been armed and motion has been detected.
  6. Sound the Alarm Loop. This loop will make the alarm sound while the mode is "Sound the Alarm". Disarming the system will change the mode and the alarm will stop sounding.

Set the Door Status Loop

This loop simply stores the value of the door (closed/open) as a true/false value in the Door Status variable. The status of the door is constantly being checked. This means that the variable is always being updated and whatever is currently stored in the variable is the current state of the door.

Sound the Alarm

As mentioned earlier, the Mode variable stores the current mode of the system. There are three modes: Disarmed, Armed, Sound the Alarm. The alarm is sounded whenever the Mode variable equals the certain value used for sounding the alarm. In this example program, the mode for Sounding the Alarm has a numeric value of 4. So, whenever the Mode equals 4, then the alarm will be sounded.

Disarming the System

Whenever the certain key color is presented to the color sensor, the mode will be set to "disarmed" and the system will say: "OK".

Trigger the Alarm

This is simple, if two conditions are met, then the alarm is triggered.

In order for the alarm system to be triggered, these two conditions must be met:

  1. The current mode needs to be: Armed
  2. Motion has to be detected

the Motion Detector

This is one of the more interresting loops in the Alarm System. First a ultrasonic sensor reading is made then the robot waits a tenth of a second and takes another ultrasonic sensor readung. If the two readings are equal to one another, then no motion has been detected and the motion variable is set to the value of "false". If the two readings are different from one another, then motion has been detected and the motion variable is set to the value of "true".

Putting It All Together

Connect two touch sensors to your NXT brick in ports #1 and #2. Port #1 will contain the touch sensor for the door. Port #3 has the Ultrasonic (motion) sensor. Port #4 has the color sensor.

Click the link below to download the source code for the Alarm System.

Download this program