Example using a PIR sensor
Dependencies: EthernetInterface mbed-rtos mbed
Fork of Seeed_Grove_PIR_Motion_Sensor_Example by
main.cpp@1:9f54cd329503, 2015-03-04 (annotated)
- Committer:
- terrence
- Date:
- Wed Mar 04 15:35:17 2015 +0000
- Revision:
- 1:9f54cd329503
- Parent:
- 0:cc930b964211
Can't get Ethernet libs to compile.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:cc930b964211 | 1 | #include "mbed.h" |
sam_grove | 0:cc930b964211 | 2 | |
sam_grove | 0:cc930b964211 | 3 | InterruptIn motion(D2); |
terrence | 1:9f54cd329503 | 4 | DigitalOut gLed(LED2); |
terrence | 1:9f54cd329503 | 5 | Serial pc(USBTX, USBRX); |
sam_grove | 0:cc930b964211 | 6 | |
sam_grove | 0:cc930b964211 | 7 | int motion_detected = 0; |
terrence | 1:9f54cd329503 | 8 | float blinkTime = .2; |
sam_grove | 0:cc930b964211 | 9 | |
sam_grove | 0:cc930b964211 | 10 | void irq_handler(void) |
sam_grove | 0:cc930b964211 | 11 | { |
sam_grove | 0:cc930b964211 | 12 | motion_detected = 1; |
sam_grove | 0:cc930b964211 | 13 | } |
terrence | 1:9f54cd329503 | 14 | |
terrence | 1:9f54cd329503 | 15 | void BlinkGreen(){ |
terrence | 1:9f54cd329503 | 16 | gLed = 0; // turn myled on |
terrence | 1:9f54cd329503 | 17 | pc.printf("LED Green \r\n"); |
terrence | 1:9f54cd329503 | 18 | wait(blinkTime); |
terrence | 1:9f54cd329503 | 19 | gLed = 1; // turn myled off |
terrence | 1:9f54cd329503 | 20 | wait(blinkTime); |
terrence | 1:9f54cd329503 | 21 | } |
sam_grove | 0:cc930b964211 | 22 | int main(void) |
sam_grove | 0:cc930b964211 | 23 | { |
terrence | 1:9f54cd329503 | 24 | pc.baud(115200); |
sam_grove | 0:cc930b964211 | 25 | int cnt = 0; |
terrence | 1:9f54cd329503 | 26 | gLed = 1; |
sam_grove | 0:cc930b964211 | 27 | motion.rise(&irq_handler); |
terrence | 1:9f54cd329503 | 28 | |
sam_grove | 0:cc930b964211 | 29 | while(1) { |
sam_grove | 0:cc930b964211 | 30 | if(motion_detected) { |
terrence | 1:9f54cd329503 | 31 | BlinkGreen(); |
sam_grove | 0:cc930b964211 | 32 | cnt++; |
sam_grove | 0:cc930b964211 | 33 | motion_detected = 0; |
terrence | 1:9f54cd329503 | 34 | pc.printf("Hello! I've detected %d times since reset\r\n", cnt); |
sam_grove | 0:cc930b964211 | 35 | } |
sam_grove | 0:cc930b964211 | 36 | } |
sam_grove | 0:cc930b964211 | 37 | } |
terrence | 1:9f54cd329503 | 38 |