Friday, 29 September 2017

How to make fence/border security system

Well, no point to be unhappy about why I didn't explain how does the sketch work, which I've already said in the all Arduino posts page. All I can say here that, this sketch get inputs from 2x ultrasonic sensors and control the servo motor. That means if anything comes within the range of one ultrasonic sensor it will direct the camera in that way.
The Sketch

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int pos = 190;    // variable to store the servo position
int trigPin1 = 6;
int echoPin1 = 7;
int trigPin2 = 8;
int echoPin2 = 9;

void setup()
{
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  Serial.begin(9600);
myservo.attach(10);  // attaches the servo on pin 10 to the servo object
}

void firstsensor(){ // This function is for first sensor.
int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

  if (distance1 <=30) {  // Change the number for long or short distances.
for (pos = 1; pos >= 0; pos -= 1) { // goes from 1 degrees to 0 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 10ms for the servo to reach the position
  }

  } else {
  secondsensor();
  }
}

void secondsensor(){ // This function is for second sensor.
int duration2, distance2;
  digitalWrite (trigPin2, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin2, LOW);
  duration2 = pulseIn (echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;


  if (distance2 <=30) {  // Change the number for long or short distances.
for (pos = 179; pos <= 180; pos += 1) { // goes from 179 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(10);                       // waits 10ms for the servo to reach the position
  }

  } else {
  firstsensor();
  }
}

void loop() {
firstsensor();
delay(500); 
}


Ad



No comments:

Post a Comment