Hello,
I am trying to use a pir motion sensor to activate a servo motor that turns 180 degrees, pauses for 5 seconds, and then turns back to its original position. I am having the hardest time writing a code for it. Would anyone be able to help or give me advice? I am mostly having trouble with the 'if else' statement.
#include <Servo.h>
int pos = 0;
int calibrationTime = 60;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 12;
int pirPos = 13;
void setup(){
myservo.attach(4); //servo to 4
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");
{
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}
void loop(){
if(digitalRead(pirPin) == HIGH){
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(10);
}
if(lockLow){
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis();
takeLowTime = false;
}