// Define the pins for the ultrasonic sensor, buzzer, and LED
const int trigPin = 18;
const int echoPin = 5;
const int buzzerPin = 4;
const int ledPin = 2;
// Variables to store the duration and distance
long duration;
int distance;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Define pins as OUTPUT or INPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Generate a 10us pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 50) {
playTone(buzzerPin, 1000, 200);
} else {
digitalWrite(buzzerPin, LOW);
}
delay(1000);
}
void playTone(int pin, int frequency, int duration) {
tone(pin, frequency);
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(duration);
noTone(pin);
digitalWrite(ledPin, LOW); // Turn off the LED
}
// // Define the pins for the ultrasonic sensor, buzzer, and LED
// const int trigPin = 18;
// const int echoPin = 5;
// const int buzzerPin = 4;
// const int ledPin = 2;
// // Variables to store the duration and distance
// long duration;
// int distance;
// void setup() {
// // Initialize the serial communication
// Serial.begin(9600);
// // Define pins as OUTPUT or INPUT
// pinMode(trigPin, OUTPUT);
// pinMode(echoPin, INPUT);
// pinMode(buzzerPin, OUTPUT);
// pinMode(ledPin, OUTPUT);
// }
// void loop() {
// // Generate a 10us pulse to trigger the ultrasonic sensor
// digitalWrite(trigPin, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin, LOW);
// duration = pulseIn(echoPin, HIGH);
// distance = duration * 0.034 / 2;
// Serial.print("Distance: ");
// Serial.print(distance);
// Serial.println(" cm");
// if (distance < 50) {
// playSiren(buzzerPin, 2000, 3000, 200);
// } else {
// digitalWrite(buzzerPin, LOW);
// }
// delay(1000);
// }
// void playSiren(int pin, int frequency1, int frequency2, int duration) {
// for (int i = 0; i < duration; i++) {
// tone(pin, frequency1);
// digitalWrite(ledPin, HIGH); // Turn on the LED
// delay(100);
// noTone(pin);
// digitalWrite(ledPin, LOW); // Turn off the LED
// delay(100);
// tone(pin, frequency2);
// digitalWrite(ledPin, HIGH); // Turn on the LED
// delay(100);
// noTone(pin);
// digitalWrite(ledPin, LOW); // Turn off the LED
// delay(100);
// }
// }
// // Define the pins for the ultrasonic sensor, buzzer, and LED
// const int trigPin = 18;
// const int echoPin = 5;
// const int buzzerPin = 4;
// const int ledPin = 2;
// // Variables to store the duration and distance
// long duration;
// int distance;
// void setup() {
// // Initialize the serial communication
// Serial.begin(9600);
// // Define pins as OUTPUT or INPUT
// pinMode(trigPin, OUTPUT);
// pinMode(echoPin, INPUT);
// pinMode(buzzerPin, OUTPUT);
// pinMode(ledPin, OUTPUT);
// }
// void loop() {
// // Generate a 10us pulse to trigger the ultrasonic sensor
// digitalWrite(trigPin, LOW);
// delayMicroseconds(2);
// digitalWrite(trigPin, HIGH);
// delayMicroseconds(10);
// digitalWrite(trigPin, LOW);
// duration = pulseIn(echoPin, HIGH);
// distance = duration * 0.034 / 2;
// Serial.print("Distance: ");
// Serial.print(distance);
// Serial.println(" cm");
// if (distance < 50) {
// playMarioTone();
// } else {
// digitalWrite(buzzerPin, LOW);
// }
// delay(1000);
// }
// void playMarioTone() {
// int melody[] = {659, 659, 0, 659, 0, 523, 659, 0, 784, 0, 392, 0, 523, 0, 392, 0};
// int noteDuration[] = {125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125};
// for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
// if (melody[i] == 0) {
// noTone(buzzerPin);
// } else {
// tone(buzzerPin, melody[i]);
// }
// delay(noteDuration[i]);
// noTone(buzzerPin);
// delay(50); // Add a small pause between notes
// }
// }