0% found this document useful (0 votes)
7 views3 pages

BSM (Blind Spot Monitoring)

The document contains Arduino code for a dynamic blind spot alert system using an ultrasonic sensor, a buzzer, and a servo motor. It defines pin configurations, system parameters, and includes functions for scanning angles and measuring distances to detect objects within a specified threshold. When an object is detected closer than 5 cm, the buzzer is activated to alert the user.

Uploaded by

amrita.kundu83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

BSM (Blind Spot Monitoring)

The document contains Arduino code for a dynamic blind spot alert system using an ultrasonic sensor, a buzzer, and a servo motor. It defines pin configurations, system parameters, and includes functions for scanning angles and measuring distances to detect objects within a specified threshold. When an object is detected closer than 5 cm, the buzzer is activated to alert the user.

Uploaded by

amrita.kundu83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Servo.

h> // Include the Servo library for motor control

// --- Pin Definitions ---


// Ultrasonic Sensor Pins
const int ULTRASONIC_TRIG_PIN = 8; // Trigger pin to send sound pulse
const int ULTRASONIC_ECHO_PIN = 9; // Echo pin to receive reflected sound

// Buzzer Pin
const int BUZZER_PIN = 2; // Pin connected to the buzzer

// Servo Motor Pin


const int SERVO_PIN = 3; // Pin connected to the servo motor signal (often PWM pin for servos)

// --- System Parameters ---


// Distance threshold for triggering the alert (in centimeters)
// NEW: Set to 25 cm for a closer detection zone.
const int THRESHOLD_DISTANCE_CM = 5; // Object closer than 25 cm triggers alert

// Servo sweep angles (degrees)


// Define the range of motion for the sensor.
// 0 degrees is typically one extreme, 180 the other. Adjust based on your setup.
const int SWEEP_START_ANGLE = 30; // Start scanning from this angle
const int SWEEP_END_ANGLE = 150; // End scanning at this angle

// Servo sweep speed control


// NEW: Lower value (5ms) for faster sweep, higher value = slower sweep.
// This is the delay between each degree step of the servo.
const int SWEEP_DELAY_MS = 5; // Delay in milliseconds between angle updates (FASTER)

// Servo step size


// How many degrees the servo moves in each step.
// 1 for smooth sweep, higher for faster but less granular steps.
const int SERVO_STEP_SIZE = 1; // Move 1 degree at a time

// --- Global Variables ---


Servo myServo; // Create a Servo object to control the motor

void setup() {
// Initialize Ultrasonic Sensor Pins
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);

// Initialize Buzzer Pin


pinMode(BUZZER_PIN, OUTPUT);

// Attach the servo object to the servo motor pin


myServo.attach(SERVO_PIN);
myServo.write(SWEEP_START_ANGLE); // Set servo to initial position

// Initialize Serial communication for debugging (important for testing!)


Serial.begin(9600);
Serial.println("--- Dynamic Blind Spot Alert System ---");
Serial.println("System Initialized. Scanning for obstacles...");
}

void loop() {
// --- Scan from Start Angle to End Angle ---
for (int angle = SWEEP_START_ANGLE; angle <= SWEEP_END_ANGLE; angle += SERVO_STEP_SIZE)
{
myServo.write(angle); // Move servo to the current angle
delay(SWEEP_DELAY_MS); // Wait for the servo to reach position

// Measure distance at the current angle


float currentDistance = getDistanceCm();

// Print current status to Serial Monitor


Serial.print("Angle: ");
Serial.print(angle);
Serial.print(" degrees | Distance: ");
Serial.print(currentDistance);
Serial.println(" cm");

// Check if an object is detected within the threshold


if (currentDistance > 0 && currentDistance < THRESHOLD_DISTANCE_CM) {
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
Serial.println("!!! ALERT: OBJECT DETECTED !!!");
// Optional: Add a short delay here to keep the buzzer on longer if needed
// delay(200); // Buzzer on for 200ms at this angle
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
}
}

// --- Scan from End Angle back to Start Angle ---


for (int angle = SWEEP_END_ANGLE; angle >= SWEEP_START_ANGLE; angle -= SERVO_STEP_SIZE) {
myServo.write(angle); // Move servo to the current angle
delay(SWEEP_DELAY_MS); // Wait for the servo to reach position

// Measure distance at the current angle


float currentDistance = getDistanceCm();

// Print current status to Serial Monitor


Serial.print("Angle: ");
Serial.print(angle);
Serial.print(" degrees | Distance: ");
Serial.print(currentDistance);
Serial.println(" cm");

// Check if an object is detected within the threshold


if (currentDistance > 0 && currentDistance < THRESHOLD_DISTANCE_CM) {
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
Serial.println("!!! ALERT: OBJECT DETECTED !!!");
// Optional: Add a short delay here to keep the buzzer on longer if needed
// delay(200); // Buzzer on for 200ms at this angle
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
}
}
}

// --- Helper Function to get distance from Ultrasonic Sensor ---


float getDistanceCm() {
// Clears the ULTRASONIC_TRIG_PIN (ensures a clean pulse)
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);

// Sets the ULTRASONIC_TRIG_PIN HIGH for 10 microseconds to send pulse


digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);

// Reads the ULTRASONIC_ECHO_PIN, returns the duration of the sound wave travel time
long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);

// Calculates the distance in centimeters


// Speed of sound = 343 meters/second = 0.0343 cm/microsecond
// Distance = (duration * speed of sound) / 2 (because sound travels to object and back)
float distance = duration * 0.0343 / 2;

// Basic filter for unrealistic readings (e.g., sensor not detecting anything)
if (distance > 400 || distance <= 0) { // HC-SR04 max range is approx 400cm
return 999; // Return a large value if out of range or bad reading
}
return distance;
}

THIS INCLUDES SERVO CODE, JUST TRY THE THING WITHOUT SERVO ONCE.
1 ULTRASONIC SENSOR
1 ACTIVE BUZZER

You might also like