Progaram Code:: Distance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

PROGARAM CODE:

#include<Servo.h>
#include<LiquidCrystal.h>

Servo myservo; // create servo object to control a servo


const int trigPin = 2;
const int buzzer=10;
const int echoPin = 4;
LiquidCrystal lcd( 12 , 11 ,5 ,4 ,3 , 2 );
int sensed;

void setup()
{
lcd.begin( 16 ,2 ); // Initialise the lcd with Row and Columns
myservo.attach(9); // attaches the servo on pin 9 to the servo object
sensed=0;
}

void loop()
{
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

cm = microsecondsToCentimeters(duration); // Covert time into


distance

if ( cm > 2 && cm < 15)


{
lcd.setCursor(0,0);
lcd.print("PLEASE WAIT");
delay(1000);
myservo.write(140); // sets the servo position according to the scaled
value
lcd.print("PLEASE ENTER");
tone(buzzer,3000);
delay(1000);
noTone(buzzer);
delay(3000);
tone(buzzer,1000);
delay(1000);

sensed = sensed + 1; //Used to count the vehicles entered

lcd.setCursor(0,0);
lcd.print("PLEASE WAIT");
}
else
{
myservo.write(40); // sets the servo position according to the scaled
value
lcd.setCursor(0,0);
lcd.print("ENTRY OPEN");

lcd.setCursor(0,1);
lcd.print(" VEHICLES : ");
lcd.setCursor(12,1);
lcd.write("sensed");

long microsecondsToCentimeters(long microseconds)


{

// The speed of sound is 340 m/s or 29 microseconds per centimeter.


// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

return microseconds / 29 / 2 ;
}

You might also like