[go: up one dir, main page]

0% found this document useful (0 votes)
45 views1 page

Const Int Trigpin 9

This document contains an Arduino code for an ultrasonic distance measuring system. It uses a buzzer and an LED to provide feedback based on the measured distance, with specific actions triggered when the distance is less than 13 cm. The code initializes pins, triggers the ultrasonic sensor, calculates distance, and controls the buzzer and LED accordingly.

Uploaded by

samadsumu9706
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)
45 views1 page

Const Int Trigpin 9

This document contains an Arduino code for an ultrasonic distance measuring system. It uses a buzzer and an LED to provide feedback based on the measured distance, with specific actions triggered when the distance is less than 13 cm. The code initializes pins, triggers the ultrasonic sensor, calculates distance, and controls the buzzer and LED accordingly.

Uploaded by

samadsumu9706
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/ 1

const int trigPin = 9;

const int echoPin = 10;


const int buzzerPin = 8;
const int ledPin = 7;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
// Trigger ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo duration


long duration = pulseIn(echoPin, HIGH, 30000);
float distance = duration * 0.034 / 2;

Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

if (distance > 0 && distance < 13) {


int delayTime = map((int)distance, 1, 13, 50, 300);

// Buzzer logic reversed here: LOW = ON, HIGH = OFF


digitalWrite(buzzerPin, LOW); // ON (inverted)
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(buzzerPin, HIGH); // OFF (inverted)
digitalWrite(ledPin, LOW);
delay(delayTime);
} else {
// Everything OFF
digitalWrite(buzzerPin, HIGH); // OFF (inverted)
digitalWrite(ledPin, LOW);
}

delay(50);
}

You might also like