Hey guys I am trying to make a code that uses a PIR motion sensor where if i wave my hand it will rotate the servo motor but the issue that I get is my motion sensor will switch back and forth for motion detected to no motion detected thus activating my servo motor when all i want it to do is activate the servo motor once after it has detected motion. Any suggestions or approaches on how do this ? I posted the code below , it works but not really. Thanks in advance everyone :)!
const int MOTION_PIN = 2;
const byte LED_PIN = 13; // LED pin - active-high
#include <Servo.h>
Servo servo1;
void setup()
{
Serial.begin(9600);
// The PIR sensor's output signal is an open-collector,
// so a pull-up resistor is required:
pinMode(MOTION_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
servo1.attach(9);
}
void loop()
{
int proximity = digitalRead(MOTION_PIN);
if (proximity == LOW) // If the sensor's output goes low, motion is detected
{
Serial.println("Motion detected!");
openDoor();
holdDoor();
closeDoor();
}
else
{
digitalWrite(LED_PIN, LOW);
Serial.println("No Motion Detected");
holdDoor();
}
}
void holdDoor(){
servo1.write(90);
delay(1000);
}
void openDoor(){
servo1.write(0);
delay(2000);
}
void closeDoor(){
servo1.write(180);
delay(2000);
}
PIR_Motion_Detector_Example.ino (887 Bytes)