I have 2 servos with a PIR sensor, I am having trouble with this code. I am very new at this........
What I am trying to do is to have the PIR sense motion then start servo #1 then start servo #2 then with a delay stop servo #1 and then stop servo #2 and repeat everytime motion is detected.
what I need it to do....
( The PIR to start servo #1, go to the set position then start servo #2, servo #2 will run until servo #1 delay runs out, then go back to start position then servo #2 will do the same..)
#include <Servo.h>
Servo mydoors;
Servo myeyes;
int posdoors = 0; //creates a servo object
int poseyes = 90; //creates a servo object
int calibrationTime = 15;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 4000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin
void setup(){
mydoors.attach(4); //attaches servo #1 to pin 4
myeyes.attach(10); //attaches servo #2 to pin 10
Serial.begin(9600); //begins serial communication
pinMode(pirPin, INPUT);
pinMode(pirPos, OUTPUT);
digitalWrite(pirPos, HIGH);
//give the sensor time to calibrate
Serial.println("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(calibrationTime - i);
Serial.print("-");
delay(1000);
}
Serial.println();
Serial.println("done");
//this waits until the PIR's output is low before ending setup
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}
void loop(){
if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo
/*turns servo from 0 to 130 degrees and back
it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 130
and setting the servo's position in degrees to "pos" every 5 milliseconds
it then does it in reverse to have it go back
to learn more about this, google "for loops"
to change the amount of degrees the servo turns, change the number 130 to the number of degrees you want it to turn
**/
for(posdoors = 0; posdoors < 130; posdoors += 100) //goes from 0 to 130 degrees
{
mydoors.write(posdoors); //tells servo to go to position in variable "pos"
delay(5000);
}
for(poseyes = 0; poseyes < 110; poseyes += 50)
myeyes.write(poseyes); //tells servo to go to position in variable "pos"
delay(5);
for(posdoors = 130; posdoors >=1; posdoors -=10) //goes from 130 to 0 degrees
{
mydoors.write(posdoors); //tells servo to go to position in variable "pos"
delay(3000);
for(poseyes = 110; poseyes >=0; poseyes -=10)
myeyes.write(poseyes); //to make the servo go faster, decrease the time in delays for
delay(2);
if(lockLow){
//makes sure we wait for a transition to LOW before further output is made
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(); //save the time of the transition from HIGH to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we can assume the motion has stopped
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
}