Love shipments from Sparkfun! |
Parallax PIR Sensor (the dome thing) |
Arduino controlled LED driven by a PIR |
Serial out from the Arduino showing data |
/*
* Motion sensing
*Started with an intro to the paralax motion sensor by Kristian Gohlke http://www.arduino.cc/playground/Code/PIRsense
*
*Then added my own stuff.
*in this case reading in a photosensor data
*
*/
//Datasheet wants 10-60sec to calibrate I don't have 60sec... I'll give it 30.
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped.. otherwise it shoots data every 5 sec.
// I considered increasing this actually.
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
//pin variables, Keep in mind I'm using an arduino mega 2560 R3
// so if you are wanting to change pins around this is the place to do it.
int pirPin = 30; //the digital pin connected to the PIR sensor's output
int ledPin = 31; // the LED pin
int phopin = 32; // the Pin for the Photo resistor
//setup like a falling pin
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(phopin, INPUT);
digitalWrite(pirPin, LOW); //this looks weird I'm sure. This means low means 0
digitalWrite(phopin, LOW); // and high means 1 I think...
//give the sensor some time to calibrate
//according to Parallax it has to get used to the environment
//I prefer to think of it as arming the system :)
Serial.println("Freeze... Don't move a muscle");
for(int i = 0; i < calibrationTime; i++){
Serial.println("tick");//it's important to see the difference between Serial.println and Serial.print the former
delay(1000); //prints then creats a newline, the latter, just prints.
}
Serial.println(" -BING!!"); //do not put three ! in a row... it's some sort of weird command...
//tells the bootloader you want a menu, and causes it to hang up when uploading
//you'll tear your hair out till you find this ~> //http://arduino.cc/forum/index.php/topic,52254.0.html
Serial.println("SENSOR ARMED"); // a little bit of nonsense now and then is relished by
delay(50); //the wisest men.
}
//loop di loop
void loop(){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false; //serial wibble wabble to show it's doing something
Serial.println("---");
Serial.print("I see you at ~ ");
Serial.print(millis()/1000);
Serial.println(" sec~");
Serial.println("ambiant light value is ");
Serial.println(analogRead(phopin));//write the value of the photoresistor to the serial monitor.
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
//and a snarky comment is made
lockLow = true;
Serial.print("I think you left at ~ "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec~");
delay(50);
}
}
}//abandon all hope ye who enter here.
Current Circuit configuration |
No comments:
Post a Comment