Last active
October 16, 2016 21:46
-
-
Save printminion/c89e04daba2cdd66af21f2c2d3688a44 to your computer and use it in GitHub Desktop.
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
| /** | |
| * @desc Arduino (MKR1000) movement detection with keyestudio PIL sensors http://wiki.keyestudio.com/index.php/Category:Sensor | |
| * @author Misha M.-Kupriyanov https://google.com/+MishaMKupriyanov | |
| * @link https://gist.github.com/printminion/c89e04daba2cdd66af21f2c2d3688a44 | |
| */ | |
| int ledPin = 13; // Connect LED to pin 13 | |
| int switcher = 2; // Connect Tilt sensor to Pin2 | |
| int bluepin = 11; // select the pin for the blue LED | |
| int redpin = 10; //select the pin for the red LED | |
| int greenpin =9;// select the pin for the green LED | |
| int val; | |
| int isSomebodyPresent; | |
| byte sensorPin = 3; //INPUT | |
| byte indicator = 13; | |
| int temt6000Pin = 0; | |
| int secondsToLight = 10000; | |
| int secondsLeft = 0; | |
| int loopDelay = 500; | |
| void setup() { | |
| Serial.begin(9600); | |
| pinMode(sensorPin,INPUT); | |
| pinMode(indicator,OUTPUT); | |
| pinMode(ledPin, OUTPUT); // Set digital pin 13 to output mode | |
| pinMode(switcher, INPUT); // Set digital pin 3 to input mode | |
| pinMode(redpin, OUTPUT); | |
| pinMode(bluepin, OUTPUT); | |
| pinMode(greenpin, OUTPUT); | |
| } | |
| void loop() | |
| { | |
| // This just reports the reading from the sensor to the serial terminal: 0-1023 with 1023 being very bright, and 0 being very dark. | |
| int value = analogRead(temt6000Pin); | |
| Serial.println(value); | |
| byte state = digitalRead(sensorPin); | |
| digitalWrite(indicator,state); | |
| if(state == 1) { | |
| isSomebodyPresent = 1; | |
| Serial.println("Somebody is in this area!"); | |
| lightWhite(); | |
| secondsLeft = secondsToLight; | |
| } else if(state == 0) { | |
| isSomebodyPresent = 0; | |
| Serial.println("No one!"); | |
| lightBlack(); | |
| } | |
| secondsLeft = secondsLeft - loopDelay; | |
| Serial.print("Milliseconds to light:"); | |
| Serial.println(secondsLeft); | |
| if (isSomebodyPresent == 0 && secondsLeft <= 0) { | |
| lightBlack(); | |
| delay(loopDelay); | |
| return; | |
| } | |
| //Read tilt sensor value | |
| if(digitalRead(switcher)==HIGH) { | |
| lightGreen(); | |
| } else { | |
| lightRed(); | |
| } | |
| delay(loopDelay); | |
| } | |
| void lightBlack() { | |
| analogWrite(bluepin, 0); | |
| analogWrite(redpin, 0); | |
| analogWrite(greenpin, 0); | |
| } | |
| void lightBlue() { | |
| analogWrite(bluepin, 255); | |
| analogWrite(redpin, 0); | |
| analogWrite(greenpin, 0); | |
| } | |
| void lightRed() { | |
| analogWrite(bluepin, 0); | |
| analogWrite(redpin, 255); | |
| analogWrite(greenpin, 0); | |
| } | |
| void lightGreen() { | |
| analogWrite(bluepin, 0); | |
| analogWrite(redpin, 0); | |
| analogWrite(greenpin, 255); | |
| } | |
| void lightWhite() { | |
| analogWrite(bluepin, 255); | |
| analogWrite(redpin, 255); | |
| analogWrite(greenpin, 255); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment