// Obstacles Detection (( Collision Prevention System ))
#include <LiquidCrystal.h>
const int trigPin = 3;
const int echoPin = 2;
const int GledPin = 12;
const int RledPin = 13;
LiquidCrystal lcd(7, 6, 5, 4, 9, 8);
const int buzzerPin = 10;
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(GledPin, OUTPUT);
pinMode(RledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
noTone(buzzerPin); // Turn off the buzzer initially
}
void loop() {
// Variables to store the duration and distance
long duration;
int distance;
// Trigger the ultrasonic sensor to measure distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Measure the echo duration
distance = duration / 29 / 2;// Calculate the distance in centimeters
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// Increase volume of the beep sensor as object gets closer
if (distance < 100) { // Adjust this threshold as needed
int toneFreq = map(distance, 0, 20, 200, 2000);
// Map distance to frequency
tone(buzzerPin, toneFreq); // Play tone with increased frequency
digitalWrite(RledPin, HIGH);
digitalWrite(GledPin, LOW);
lcd.setCursor(0, 1);
lcd.print("Please STOP Now!");
} else {
noTone(buzzerPin); // Turn off the buzzer if no object is close
digitalWrite(RledPin, LOW);
digitalWrite(GledPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("You can Move Now");
}
delay(100);
}
// To create a Arduino Uno program (commonly referred to as a sketch)
// that uses an ultrasonic sensor to measure distance,
// displays the distance on a 4-bit LCD, and increases
// the volume of a beep sensor as an object gets closer
// to the ultrasonic sensor, you can follow the steps below.
// This example assumes you have a 4-bit LCD and a beep sensor
// (like a piezo buzzer) connected to your Arduino Uno.
// ### Materials Needed:
// 1. Arduino Uno board
// 2. Ultrasonic Sensor (e.g., HC-SR04)
// 3. 4-bit LCD display (e.g., 16x2 LCD)
// 4. Beep sensor (e.g., Piezo Buzzer)
// 5. Jumper wires
// ~~~~~~~~~~~~~~~~
// Sketch
// ~~~~~~~~~~~~~~~~~~
// ### Wiring:
// 1. Connect the VCC and GND of the ultrasonic sensor
// to 5V and GND on the Arduino, respectively.
// 2. Connect the Trig pin of the ultrasonic sensor to
// pin 2 on the Arduino.
// 3. Connect the Echo pin of the ultrasonic sensor to
// pin 3 on the Arduino.
// 4. Connect the VCC and GND of the 4-bit LCD to 5V
// and GND on the Arduino, respectively.
// 5. Connect the RS, EN, D4, D5, D6, and D7 pins of
// the LCD to pins 7, 6, 5, 4, 9, and 8 on the Arduino, respectively.
// 6. Connect the positive (anode) lead of the beep sensor
// (Piezo Buzzer) to pin 10 on the Arduino.
// 7. Connect the negative (cathode) lead of the beep sensor
// to GND on the Arduino.
// Make sure you have the necessary libraries installed,
// such as the "LiquidCrystal" library for the LCD.
// You can install it through the Arduino Library Manager.
// Upload the sketch to your Arduino Uno,
// and you should be able to measure distances
// with the ultrasonic sensor, display them on the LCD,
// and increase the volume of the beep sensor as objects
// get closer to the ultrasonic sensor. Adjust the
// distance threshold and tone frequency mapping to
// suit your specific needs.