Game plan was simple, send some serial data once the PIR detects a person. seemed simple enough, PIR was in the repeat trigger mode. and I used repeat trigger mode, because system should ALWAYS be aware if the person is there or not. However, I still needed a timeout for turning off the camera and mic, or else it will keep turning everything off all the time. (PIR get some wrong readings and garbage values at times.) therefore, the code waits for 30 seconds until the person isn't there to turn the camera and mic off.
I had to manage the serial data that was being sent to the PC. we obviously do not need the data all the time, just when we detect / undetected someone. Therefore, only 5 lines were sent thru serial line to the PC. and used strings as opposed to binary values (1 or a 0) just so that we can confirm that its the correct data and not some random value,
That was it for the Arduino section. Code is below
int PIRSensor = 12;
unsigned long timeInLoop = 0;
unsigned long elapsedTime = 0;
void setup()
{
Serial.begin(9600);
pinMode(PIRSensor, INPUT_PULLUP);
}
void loop()
{
int count = 0;
while (digitalRead(PIRSensor))
{
if (count < 5)
{
Serial.println("PERSON DETECTED");
}
count = count + 1;
delay(1);
}
unsigned long currentTime = millis();
while (!digitalRead(PIRSensor))
{
timeInLoop = millis();
elapsedTime = timeInLoop - currentTime;
if (elapsedTime > 30000)
{
Serial.println("NO PERSON DETECTED");
}
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.