//Project name - Automation of railway crossing to prevent accidents on the track between train and any other vechile
//Project by - Kunwar Ranveer
//Class - 8
//Student of Denobili School, CMRI, Dhanbad, Jharkhand.
//Email ID - [email protected]
//Contact no. - +91 9304152407
#include <Servo.h>//Library for servo motor is required naming (servo)
Servo servo1;
//defining of all the pins
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
#define Led1 13
#define Led2 12
int Buzzer = 6;
void setup()
{
servo1.attach(7);
pinMode(trigPin, OUTPUT);//Sensor
pinMode(echoPin, INPUT);//Sensor
pinMode(Led1, OUTPUT);//Red light
pinMode(Led2, OUTPUT);//Green light
pinMode(Buzzer, OUTPUT);//Buzzer
}
void loop()
{
ultra_sonic();
servo1.write(90);
if(distance <=300)//distance is less than 300
{
servo1.write(270);
digitalWrite(Led1, HIGH );//Switches on Red light when distance is less than 300
digitalWrite(Led2, LOW);//Switches off Green light when distance is less than 300
digitalWrite(Buzzer, HIGH);//Rings the buzzer to indicate that the gate is closing and train is coming
}
if(distance >=300)//distance is more than 300
{
digitalWrite(Led1, LOW);//Switches off Red light when distance is more than 300
digitalWrite(Led2, HIGH);//Switches on Green light when distance is more than 300
digitalWrite(Buzzer, LOW);//Rings the buzzer to indicate that the gate is opening and train has gone
}
}
void ultra_sonic()
{
//control of Ultra-sonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}