/*
Component Component pin Arduino Nano
Front HC-SR04 TRIG D2
Front HC-SR04 ECHO D3
Left HC-SR04 TRIG D4
Left HC-SR04 ECHO D5
Right HC-SR04 TRIG D6
Right HC-SR04 ECHO D7
Piezo buzzer Signal / + D8
Red LED Anode through 220 Ω resistor D9
Blue LED Anode through 220 Ω resistor D10
Start/Stop button One terminal D11
Start/Stop button Other terminal GND
I²C LCD SDA A4
I²C LCD SCL A5
All three HC-SR04 modules VCC 5V
All three HC-SR04 modules GND GND
I²C LCD VCC 5V
I²C LCD GND GND
Buzzer Negative / − GND
Red LED Cathode GND
Blue LED Cathode GND
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ---------------- LCD ----------------
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ---------------- Ultrasonic Sensors ----------------
const byte FRONT_TRIG = 2;
const byte FRONT_ECHO = 3;
const byte LEFT_TRIG = 4;
const byte LEFT_ECHO = 5;
const byte RIGHT_TRIG = 6;
const byte RIGHT_ECHO = 7;
// ---------------- Warning Outputs ----------------
const byte BUZZER_PIN = 8;
const byte RED_LED_PIN = 9;
const byte BLUE_LED_PIN = 10;
// ---------------- Start/Stop Button ----------------
const byte BUTTON_PIN = 11;
// ---------------- Settings ----------------
const int ALERT_DISTANCE_CM = 30;
const unsigned long ECHO_TIMEOUT_US = 25000UL;
const unsigned long DEBOUNCE_MS = 40;
// ---------------- State ----------------
bool monitoring = false;
bool lastButtonReading = HIGH;
bool stableButtonState = HIGH;
unsigned long lastDebounceTime = 0;
int frontDistance = -1;
int leftDistance = -1;
int rightDistance = -1;
// --------------------------------------------------
// Read one HC-SR04 sensor.
// Returns distance in cm, or -1 if no echo is received.
// --------------------------------------------------
int readDistance(byte trigPin, byte echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(3);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
unsigned long duration =
pulseIn(echoPin, HIGH, ECHO_TIMEOUT_US);
if (duration == 0) {
return -1;
}
int distance = duration / 58;
if (distance < 2 || distance > 400) {
return -1;
}
return distance;
}
// --------------------------------------------------
// Read all sensors sequentially to reduce cross-talk.
// --------------------------------------------------
void readAllSensors() {
frontDistance = readDistance(FRONT_TRIG, FRONT_ECHO);
delay(60);
leftDistance = readDistance(LEFT_TRIG, LEFT_ECHO);
delay(60);
rightDistance = readDistance(RIGHT_TRIG, RIGHT_ECHO);
delay(60);
}
// --------------------------------------------------
// Check whether a valid reading indicates an obstacle.
// --------------------------------------------------
bool obstacleDetected(int distance) {
return distance >= 2 &&
distance <= ALERT_DISTANCE_CM;
}
// --------------------------------------------------
// Update buzzer and both LEDs.
// --------------------------------------------------
void updateAlerts() {
bool alert =
obstacleDetected(frontDistance) ||
obstacleDetected(leftDistance) ||
obstacleDetected(rightDistance);
if (!monitoring || !alert) {
noTone(BUZZER_PIN);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
return;
}
// All three warnings activate together.
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BLUE_LED_PIN, HIGH);
tone(BUZZER_PIN, 2000);
}
// --------------------------------------------------
// Display distance as three characters.
// Invalid readings appear as "---".
// --------------------------------------------------
void printDistance(int distance) {
if (distance < 0) {
lcd.print("---");
} else if (distance < 100) {
if (distance < 10) {
lcd.print('0');
}
lcd.print('0');
lcd.print(distance);
} else {
lcd.print(distance);
}
}
// --------------------------------------------------
// Find the nearest valid sensor reading.
// --------------------------------------------------
int nearestDistance() {
int nearest = -1;
if (frontDistance >= 0) {
nearest = frontDistance;
}
if (leftDistance >= 0 &&
(nearest < 0 || leftDistance < nearest)) {
nearest = leftDistance;
}
if (rightDistance >= 0 &&
(nearest < 0 || rightDistance < nearest)) {
nearest = rightDistance;
}
return nearest;
}
// --------------------------------------------------
// LCD display.
// Line 1: F:xxx L:xxx
// Line 2: R:xxx and overall status.
// --------------------------------------------------
void updateLCD() {
lcd.setCursor(0, 0);
if (!monitoring) {
lcd.print("MONITORING OFF ");
lcd.setCursor(0, 1);
lcd.print("Press to start ");
return;
}
lcd.print("F:");
printDistance(frontDistance);
lcd.print(" L:");
printDistance(leftDistance);
lcd.setCursor(0, 1);
lcd.print("R:");
printDistance(rightDistance);
lcd.print(' ');
bool alert =
obstacleDetected(frontDistance) ||
obstacleDetected(leftDistance) ||
obstacleDetected(rightDistance);
if (!alert) {
lcd.print("CLEAR");
} else {
// Identify the closest detected obstacle.
int nearest = nearestDistance();
if (nearest == frontDistance &&
obstacleDetected(frontDistance)) {
lcd.print("ALRT:F");
} else if (nearest == leftDistance &&
obstacleDetected(leftDistance)) {
lcd.print("ALRT:L");
} else if (nearest == rightDistance &&
obstacleDetected(rightDistance)) {
lcd.print("ALRT:R");
} else {
lcd.print("ALERT");
}
}
// Clear any leftover characters from the previous frame.
lcd.print(" ");
}
// --------------------------------------------------
// Debounced toggle button.
// INPUT_PULLUP means LOW = pressed.
// --------------------------------------------------
void handleButton() {
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonReading) {
lastDebounceTime = millis();
}
if (millis() - lastDebounceTime >= DEBOUNCE_MS) {
if (reading != stableButtonState) {
stableButtonState = reading;
if (stableButtonState == LOW) {
monitoring = !monitoring;
if (!monitoring) {
noTone(BUZZER_PIN);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
}
updateLCD();
}
}
}
lastButtonReading = reading;
}
// --------------------------------------------------
// Setup
// --------------------------------------------------
void setup() {
pinMode(FRONT_TRIG, OUTPUT);
pinMode(FRONT_ECHO, INPUT);
pinMode(LEFT_TRIG, OUTPUT);
pinMode(LEFT_ECHO, INPUT);
pinMode(RIGHT_TRIG, OUTPUT);
pinMode(RIGHT_ECHO, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(FRONT_TRIG, LOW);
digitalWrite(LEFT_TRIG, LOW);
digitalWrite(RIGHT_TRIG, LOW);
noTone(BUZZER_PIN);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Smart Navigation");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(1500);
lcd.clear();
updateLCD();
}
// --------------------------------------------------
// Main loop
// --------------------------------------------------
void loop() {
handleButton();
if (monitoring) {
readAllSensors();
updateAlerts();
updateLCD();
} else {
// Keep all warning outputs off while stopped.
noTone(BUZZER_PIN);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
}
}
Haptic Motor
Front
Left
Right