0% found this document useful (0 votes)
22 views2 pages

Code For Automatic Rail-Gate

The document contains code for an Arduino project that uses an ultrasonic sensor and servos. It measures distance with the ultrasonic sensor and controls two servos and LEDs based on the distance reading, triggering alarms and moving the servos if an object is close.

Uploaded by

MD Abdur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Code For Automatic Rail-Gate

The document contains code for an Arduino project that uses an ultrasonic sensor and servos. It measures distance with the ultrasonic sensor and controls two servos and LEDs based on the distance reading, triggering alarms and moving the servos if an object is close.

Uploaded by

MD Abdur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Code

#include <Servo.h>
Servo servo1, servo2;
int echoPin = 2;
int trigPin = 3;
int buzzer = 4;
int ledr = 5;
int ledy = 6;
int ledg = 7;
long distance;
long duration;

void setup()
{
pinMode(ledr, OUTPUT);
pinMode(ledy, OUTPUT);
pinMode(ledg, OUTPUT);
pinMode(buzzer, OUTPUT);
servo1.attach(8);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
servo2.attach(9);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop()
{
ultra_sonic();
servo1.write(100);
servo2.write(100);
if(distance <=10)
{
digitalWrite(buzzer, HIGH);
digitalWrite(ledy, HIGH);
digitalWrite(ledr, LOW);
digitalWrite(ledg, LOW);
delay(1000);

servo1.write(180);
servo2.write(10);
digitalWrite(buzzer, HIGH);
digitalWrite(ledy, LOW);
digitalWrite(ledr, HIGH);
digitalWrite(ledg, LOW);
delay(1000);

digitalWrite(buzzer, LOW);
digitalWrite(ledy, LOW);
digitalWrite(ledr, LOW);
digitalWrite(ledg, HIGH);
delay(1000);
}

else if(distance>=10)
{
digitalWrite(ledr, LOW);
digitalWrite(ledy, LOW);
digitalWrite(ledg, HIGH);
digitalWrite(buzzer, LOW);
}

}
void ultra_sonic()
{
digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;
}

You might also like