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

Arduino Code

This document contains an Arduino code for measuring distance using ultrasonic sensors. It triggers the sensor, calculates the distance in centimeters, and activates an LED and buzzer if the distance is within a specified range. The code also includes a function to convert microseconds to centimeters for accurate distance measurement.

Uploaded by

sankhyaanishan
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)
4 views2 pages

Arduino Code

This document contains an Arduino code for measuring distance using ultrasonic sensors. It triggers the sensor, calculates the distance in centimeters, and activates an LED and buzzer if the distance is within a specified range. The code also includes a function to convert microseconds to centimeters for accurate distance measurement.

Uploaded by

sankhyaanishan
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

const int pingTrigPin = A4; //Trigger connected to PIN

const int pingEchoPin = A5; //Echo connected yo PIN


int led=13; //Buzzer to PIN 4
int buz1=9;
void setup()
{[Link](9600);
pinMode(led, OUTPUT);
pinMode(buz1, OUTPUT);
pinMode(pingTrigPin, OUTPUT);
pinMode(pingEchoPin, INPUT); }
void loop()
{
long duration, cm;
digitalWrite(pingTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigPin, LOW);
duration = pulseIn(pingEchoPin, HIGH);
cm = microsecondsToCentimeters(duration);
if(cm<=100 && cm>0)
{
int d= map(cm, 1, 300, 10, 1000);
digitalWrite(led, HIGH);
digitalWrite(buz1, HIGH);
delay(50);
digitalWrite(led, LOW);
digitalWrite(buz1, LOW);
delay(d);
}
[Link](cm);
[Link]("cm");
[Link]();
delay(40);
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}

You might also like