I have an Arduino set up with multiple sensors printing to serial port. Initially, I had the PIR sensor implemented with several delays built in so it would not spam the motion detected message. However, the delay() function, of course, pauses the whole code and interferes with the other sensors.
What I want to do:
1)When the motion sensor is high for x amount of time, print ONE message to the serial
2)Insert a brief period of time here where it does not detect motion
3)If the motion sensor is high for time < x it does nothing
The general idea being to use software to minimize false positives as well. I've been trying to use the millis() function to control it, but haven't had much success. Its probably a really simple solution and I'm just not seeing it.
void loop() {
// Get value from motion sensor
pirValue = digitalRead(pirPin); //detectedPin
// See if motion Detected
unsigned long currentMillis = millis();
if (pirValue == 1 && (currentMillis - previousMillis >= interval) ){
previousMillis = millis();
// Display Triggered LED for 3 seconds
digitalWrite(detectedLED, HIGH);
motionDetected = 1;
Serial.println("motion detected");
//delay(3000);
} else {
digitalWrite(detectedLED, LOW);
}
I know this is absolutely wrong, as I think the way its set now the loop will never get entered. I've tried several other structures, but I'm not seeing what I need to do. Is this a situation where I'd need to construct a state machine essentially? Any help thinking through this would be appreciated!