// Import the necessary libraries
#include <Ultrasonic.h>
// Define the pins for the ultrasonic sensor
const int trigPin = 4;
const int echoPin = 5;
// Create an ultrasonic sensor object
Ultrasonic ultrasonic(trigPin, echoPin);
// Define the buzzer pin
const int buzzerPin = 1;
const int led1=2;
const int led2=3;
void setup() {
// Set the buzzer pin as output
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Get the distance from the ultrasonic sensor
float distance = ultrasonic.read();
Serial.print("Distance is: ");
Serial.print(distance);
Serial.println("cm");
// If the distance is less than 10 cm, turn on the buzzer
if (distance < 10) {
digitalWrite(led2, HIGH);
tone(buzzerPin,600);
} else {
digitalWrite(buzzerPin, LOW);
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
}
delay(500);
}