//DeafGuard Project
//In the simulation of this program, the Wokwi platform was used.
//The very basic components such as the transistor of which we need in building our device is not on the platform.
//But this code I believe would work, if our calculations are done right.
//A vibration motor is also not in the platform, but using a LED in place of it, helps us understand the logic of fthe code.
// Ultrasonic sensor pins
const int trigPin = 3; // Trig pin connected to D3
const int echoPin = 2; // Echo pin connected to D2
// Vibration motor pin
//(Using a led in place of the motor, becuase there is no vibration motor in Wokwi)
const int ledPin = 10; // Motor connected to D10
// Distance threshold in centimeters (2.10 meters)
const int thresholdDistance = 210;
void setup() {
// Initialize serial communication (optional for debugging)
Serial.begin(115200);
// Set up the pin modes for ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Set up the pin mode for the vibration motor
pinMode(ledPin, OUTPUT);
// Start with the vibration motor off
digitalWrite(ledPin, LOW);
}
void loop() {
long duration, distance;
// Send a pulse from the ultrasonic sensor's Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the pulse duration from the Echo pin
duration = pulseIn(echoPin, HIGH);
// Calculate distance in centimeters (duration in microseconds / 58 = distance in cm)
distance = duration / 58;
// Print distance for debugging (optional)
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If the object is within the threshold distance, activate the vibration motor
//(Using a led in place of the motor, becuase there is no vibration motor in Wokwi)
if (distance > 0 && distance <= thresholdDistance) {
digitalWrite(ledPin, HIGH); // Turn on the motor
} else {
digitalWrite(ledPin, LOW); // Turn off the motor
}
// Short delay before next measurement
delay(200); // 200 milliseconds
}