Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Turn Your PC Into a Motion Sensing Security Device with .NET  : Page 2

Thanks to new types of hardware that can easily be programmed using .NET, you can create a motion sensing application that can be deployed for security—or just for fun.


advertisement
Programming the Sensors
To program the sensors, you need to use the PBASIC (Parallax Basic) language. Parallax provides the free BASIC Stamp Windows Editor (note: link opens a download dialog) that makes programming the BS2 very easy.

Once the BASIC Stamp Windows Editor is downloaded and installed, you can launch it (see Figure 8).




Figure 8. The free BASIC Stamp Windows Editor will help you program the sensors.
 
Figure 9. To write the program, take note of the two buttons labeled 1 and 2 on the free BASIC Stamp Windows Editor UI, shown.

Programming PBASIC is not difficult, and I shall explain the syntax as I go along. For now, let's program the PIR sensor to detect motion.

The first step is to add a PBASIC BS2 directive to your program to indicate to the editor that your program is going to run on a BS2 module. You can insert the following directive by clicking on the (1) button shown in Figure 9:

' {$STAMP BS2}

Next, you need to insert the PBASIC 2.5 directive to specify the version of PBASIC you are using. Button (2) (see Figure 9) will insert the following directive:

' {$PBASIC 2.5}

You want to continually get the output from the PIR sensor so that you can detect any motions that occur. In this case, use the DO LOOP command:

DO DEBUG BIN IN14 LOOP

The DEBUG command will send a value (or string) back to the BASIC Stamp Windows Editor (on the PC). This command is useful for debugging your program running on the BS2. In this case I'm printing a binary value (BIN) for the pin number 14 (IN14; recall that my PIR sensor is connected to servo port 14) of the BS2.

Click on the Run icon (see Figure 9) to download the program onto the BS2. The Debug Terminal will appear and display the output from the BS2 (see Figure 10). The PIR sensor takes about 60 seconds to warm up and stabilize. After that, try making some movements in front of it and you will notice that the output changes from 0 to 1. Any change from 0 to 1 indicates movement detected by the PIR sensor.


Figure 10. Output generated by the PIR sensor shows 0 for new movement and 1 for movement.
 
Figure 11. When testing the PING))) sensor you can see that it's working because the output gets smaller as my test object gets closer to the sensor.

Now that the PIR sensor is programmed, I'll turn my attention to the PING))) sensor, which is more challenging. First, you need to understand how to activate the sensor. To activate the PING))) sensor, you need to send a low-high-low pulse to trigger it. After it is triggered, it will wait for about 200 microseconds before it sends out an ultrasonic burst. In the meantime, you will wait for the burst to be bounced back and this entire pulse duration (started when the burst is sent) represents the round-trip distance from the sensor to the object you are detecting.

In the BS2, a pulse is defined to be 2µs (two micro-seconds). Hence to convert the pulse to time, you multiply the number of pulse by 2. This will give you the time in µs. As the measured pulse is the total round-trip time, you need to divide it by 2 to get only the time from the sensor to the detected object.

As sound travels through air at 1,130 feet per second (at sea level), this works out to be 1 inch in 73.746 µs, or 1 cm in 29.034 µs. To convert the time in µs to distance in cm, you need to multiply the time by 29.034 (or 30 for simplicity). This will give you the distance in cm. The following BS2 code summarizes what I have just described:

' {$STAMP BS2} ' {$PBASIC 2.5} '---duration of the trigger; 1 represents 2 micro-seconds--- Trigger CON 1 '---CON represents constant--- '---variable to measure the pulse--- rawDist VAR Word '---VAR represents variable--- '---the I/O Pin connected to the PING))) sensor--- Ping PIN 15 '---PIN represents pin on the BS2--- DO '---Set the pin to low first--- Ping = 0 '---trigger the sensor by sending a pulse --- PULSOUT Ping, Trigger '---measure the echo pulse by reading it--- PULSIN Ping, 1, rawDist '---convert pulses to micro-seconds--- rawDist = rawDist * 2 '---get the single-trip timing--- rawDist = rawDist / 2 '---convert the distance to cm--- rawDist = rawDist /30 '---print out the distance in cm--- DEBUG DEC rawDist, CR '---delay for 100 milliseconds--- PAUSE 100 LOOP

Author's Note: The above is modified from the sample provided by Parallax (http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf). Do note that Basic Stamp is only able to deal with Integer division; for complex floating point division, check out the language reference of PBASIC.
Note:

Test the program by clicking the Run icon. Slowly move an object towards the PING))) sensor and you will see that the distance gets smaller and smaller (see Figure 11).



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap