2014年7月24日 星期四

Using LiveCode on Raspberry Pi – Demo 1 : PIR sonsor & LED (English)

    Raspberry Pi has 26 GPIO pins on it (it will be up to 40 pins on new RPi Model B+). It can controls the LEDs, sensors, motors and many others. In this article I’m going to demo a program writing in LiveCode sciprts to detect motions using PIR motion sensor. When sensor detect motions light up the LED. When that motion is gone LED went dark.

    Here’s the demo vidieo : http://youtu.be/ZLLmuAAM7Cc
 
    In my script, I set the program to wait 2 seconds for each detection(Which some tutorial on the web suggests). That’s why you could possibly feel the delays of the sensor’ detection in the video. My LiveCode script in this demo are opened. Maybe you could try yourself to modify the waiting time. To find the best time setting.

    Here’s the electrical circuits:
    There circuits here are not exactly the same us the circuits you can see in the photo or video. I make it as simple as it can. 
    First, connect PIR to our RPi. Connect PIR-VCC (red) to 5V pin and PIR-GND(black) to any GND pin on RPi. Connect PIR-OUT (yello) to GPIO18 pin. You HAVE TO connect to GPIO 18 cause the program is written to control GPIO 18 pin. 
    Second, plug LED cathode to resistor on breadboard. The other pin of resistor connect to GND pin. Remember to connect LED anode the GPIO 4 which the program sets.

    In this demo I offer two LiveCode externals. You can download them here :

    Unzip the file.Run “haoRPiDemo1.rev”.
    
    If you move the three files to other place. You should reset the external as following steps. Put “haorpiled.so” and “haorpipir.so” into the proper direction let’s say “/home/pi/haopir”.Open the demo program “haoRPiDemo1.rev” .Set the external of this stack to the both file path. You can use the Inspector to set the External References, either.
    Save stack , restart LiveCode IDE just to be sure the library load into our stack file.

    When the stack is open. First press button “setup” to open the GPIO connection. If everything goes well the message box will display message “OK”. Then press button “Start Detect” the PIR sensor will start the detection.When PIR dectect motion LED will light up.
    Scripts are simple. Take LED for example. First use “haoRPiLEDOpen()” to setup. Function “haoRPiLEDLight()” to light up LED,”haoRPiLEDDark()” to shut down LED.For PIR sensor, after setup you can use “haoRPiPIRReadMotion()” to read values. 1 means motion detect, 0 means no motion detect. When LiveCode did read a value, it will send a message “PIRSensorDidReadValue” with the read value as first parameter.At the end of the program you should close the GPIO connections.Use function “haoRPiPIRClose” and “haoRPiLEDClose” to accomplish that.
        Remember all the function you can use is defined as function in LiveCode(instead of command), It will return “OK” if everything goes well.

That’s all ! Thanks for watching.
If you find any bugs.That me know. J

中文版連結:中文版

2 則留言: