So I just set up a board with a pir and range finder. I have both of them working and outputting to my computer. However the red(high) light for the pir only stays on as long as there is motion. I want it to stay on as long as the arduino recognizes motion (5000) after last motion here is the code I would love any help you can give.
int calibrationTime = 30; //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
long unsigned int lowIn; //the time when the sensor outputs a low impulse
long unsigned int pause = 5000; //the amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 8; //the digital pin connected to the Motion Sensor Output
int ledPin = 13; //LED pin to denote movement
int ledPin2 = 2; //LED pin to denote movement
int pingPin = 12; //the digital pin connected to the range finder
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
digitalWrite(pirPin, LOW);
Serial.print("calibrating sensor ");//give the sensor some time to calibrate
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop(){
long duration, inches, cm; // establish variables for duration of the ping, and the distance result in inches and centimeters
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin2, LOW); //the led visualizes the sensors output pin state
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.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin2, HIGH); //the led visualizes the sensors output pin state
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(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW); // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
delayMicroseconds(200); // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(pingPin, HIGH);
delayMicroseconds(500);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}