// First let's think about your use case...
// 1. What should be the time between each round of detection for your purpose?
// I have it here 3 miliseconds (3 seconds) so in every 3 seconds the sensor will give an output
int delayTime = 3000;
// 2. How long do you want the last reading to remain valid, even if the person is no longer moving?
// For our project, we want the detection of presence to remain valid for 45 seconds.
// Hence, we calculate the lagValue to be 45s / delayTime = 15
int lagValue = 15;
// 3. How close should the people be from the sensor to be consider "present"?
// Here, we have it at 50 cm
distThreshold = 50;
// Now lets write these to prepare the arduino for its job!
// Define pins
int ledPin = 13; // LED - we will use this to symbol the action being triggered from detected presence
int pirPin = 2; // pin 2 is called pirPin, communicating output from the infrared motion sensor
#define TRIG_PIN 12 // pin 12 is called TRIG_PIN
#define ECHO_PIN 11 // pin 11 is called ECHO_PIN
// Utilize existing library
#include "SR04.h" // include "the h file" in the "HC-SR04" library
// Now we will define some helpers here
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); // This creates a shorthand "sr04" that is equivalent to "SR04(ECHO_PIN,TRIG_PIN)"
int pirStat = 0; // lets define a variable to represent the infrared motion status. It will be set to 0 as default.
long dist; // lets define a variable to hold the value of the distance measured from the ultrasonic sensor.
int pValue = 0; // lets define a presence value "pValue" that is default to 0 for the calculation of the presence lag.
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ledPin, OUTPUT); // initializes digital pin named ledPin as output
pinMode(pirPin, INPUT); // initializes pirPin as input
Serial.begin(9600); // the baud rate is set to 9600. make sure to match this on the serial monitor
}
void loop(){
dist = sr04.Distance(); // Grab the distance result from the "sr04" function and store it in the long variable "dist
pirStat = digitalRead(pirPin); // store the status of infrared sensor in pirStat
if (pirStat == HIGH && dist < distThreshold) { // if the infrared sensor detected motion and the distance between the object and the ultrasonic sensor is within the predetermined range
Serial.println("Motion detected"); // display in serial monitor that motion has been detected
Serial.println(dist); // show the distance
pValue = lagValue; // default the count down for "last present" back to the beginning
}
else { // if the infrared sensor did not detect motion or the object is out of range
Serial.println("No motion detected"); // display in serial monitor that motion has not been detected
pValue = pValue-1; // start the count down
}
if (pValue > 0) { // if the lag of presence is not over
digitalWrite(ledPin, HIGH); // action will be triggered
delay(delayTime); // the action will last until the next round of detection
}
else { // if the lag of presence, in our case, after 45 seconds, is over
digitalWrite(ledPin, LOW); // action will stop
delay(delayTime); // the action will be in halt until the next round of detection
}
}