In: Physics
You need the following components to design this system:
Nano Arduino
Battery
Battery Connector
Ultrasonic sensor
Buzzer or similar alert system
First connect ultrasonic sensor to arduino. Then connect this system to the battery using connector. Connect this to the buzzer.
please find the code below:
## defining the pins connection
const int trigPin = 12;
const int echoPin = 11;
const int buzzerPin = 13;
// defines variables
int setDistance=20;
int GetDistance()
{
long duration;
int distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
return distance;
}
void setup() {
// put your setup code here, to run once:
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(trigPin, OUTPUT); // trigPin as an Output
pinMode(echoPin, INPUT); // echoPin as an Input
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
delay(1000);
//setDistance= GetDistance();
//setDistance=8;
}
void loop() {
Serial.print(“Current Distance: “);
Serial.print(GetDistance());
Serial.print(” Set Distance: “);
Serial.println(setDistance);
if(GetDistance()<setDistance-1)
{
digitalWrite(buzzerPin, HIGH); // LED on
}
else
{
digitalWrite(buzzerPin, LOW); // LED light on
}
delay(5000);
}