Created
November 5, 2018 19:38
-
-
Save bouchtaoui-dev/74b3ad0bb361d907b5ac8460ffcc7deb to your computer and use it in GitHub Desktop.
Voorbeeld code van een bewegingsdetectie voor demonstratie van Home Automation met de Raspberry Pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <signal.h> | |
| #include <stdio.h> | |
| #include <wiringPi.h> | |
| #define LED 4 | |
| #define PIR 5 | |
| volatile char isRunning = 1; | |
| void processMotionDetection(); | |
| static void interruptHandler(const int signal); | |
| int main() { | |
| // registreer interrupt signaal met de bijbehorende handler | |
| signal(SIGINT, interruptHandler); | |
| // doe een setup om wiringPi te gebruiken | |
| if(wiringPiSetup()<0) { | |
| printf("Error setting up wiringPi..."); | |
| return -1; | |
| } | |
| // configureer de gewenste pinnen | |
| pinMode( LED, OUTPUT ); | |
| pinMode( PIR, INPUT ); | |
| printf("Pin 4 is configured as output\n"); | |
| printf("Pin 5 is configured as input\n"); | |
| while(isRunning) { | |
| // 1. check for motion | |
| delay(100); | |
| if(digitalRead(PIR)) { | |
| processMotionDetection(); | |
| } | |
| } | |
| digitalWrite(LED, LOW); | |
| pinMode(LED, INPUT); | |
| printf("\nProgram finished... thank you :)\n"); | |
| return 0; | |
| } | |
| void processMotionDetection() { | |
| printf("Motion detected!\n"); | |
| printf("LED turned on!\n"); | |
| digitalWrite(LED, HIGH); | |
| int timeout = 6; | |
| int motion = HIGH; | |
| while( motion || timeout ) { | |
| motion = digitalRead(PIR); | |
| if(motion) | |
| timeout = 6; | |
| else | |
| timeout--; | |
| delay(1000); | |
| } | |
| digitalWrite(LED, LOW); | |
| printf("LED turned off!\n"); | |
| } | |
| static void interruptHandler(const int signal) { | |
| isRunning = 0; | |
| } | |
| /* | |
| pi@raspberrypi:~/development/C $ gpio -v | |
| gpio version: 2.46 | |
| Copyright (c) 2012-2018 Gordon Henderson | |
| This is free software with ABSOLUTELY NO WARRANTY. | |
| For details type: gpio -warranty | |
| Raspberry Pi Details: | |
| Type: Model B, Revision: 02, Memory: 512MB, Maker: Sony | |
| * Device tree is enabled. | |
| *--> Raspberry Pi Model B Rev 2 | |
| * This Raspberry Pi supports user-level GPIO access. | |
| pi@raspberrypi:~/development/C $ gpio readall | |
| +-----+-----+---------+------+---+-Model B1-+---+------+---------+-----+-----+ | |
| | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | | |
| +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | |
| | | | 3.3v | | | 1 || 2 | | | 5v | | | | |
| | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | | | |
| | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | | | |
| | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 | | |
| | | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 | | |
| | 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 | | |
| | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | | | |
| | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 | | |
| | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 | | |
| | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | | | |
| | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 | | |
| | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 | | |
| | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 | | |
| +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | |
| | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | | |
| +-----+-----+---------+------+---+-Model B1-+---+------+---------+-----+-----+ | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment