i have a gate system project which is connected PIR and servo motor to the arduino board. The arduino is then connected to the VB.NET. Here is my question, how can i close the gate (servo motor as my gate) once the motion is detected by the PIR? Below is my coding.
#include <Servo.h>
Servo myservo; //creates a servo object
Servo myservo2; //a maximum of eight servo objects can be created
//amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)
int calibrationTime = 30;
//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 = 5000;
int pirPin = 12; //digital pin connected to the PIR's output
int pirPos = 13; //connects to the PIR's 5V pin
void setup(){
myservo.attach(5); //attaches servo to pin 4
myservo2.attach(3);
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");
while (digitalRead(pirPin) == HIGH) {
delay(500);
Serial.print(".");
}
Serial.print("SENSOR ACTIVE");
}
void loop(){
int pos;
if (Serial.available()>0){
pos=Serial.read();
if(pos=='0'){
openGate();
}
}
if(digitalRead(pirPin) == HIGH) //turn servo
{
closeGate();
}
}
void closeGate(){
myservo.write(90);
myservo2.write(90);
}
void openGate(){
myservo.write(-90);
myservo2.write(-90);
delay(60000);
}
Appreciate for the help. Thanks