Distance Alert
This Distance Alert uses ultra-sonic to measure distance between the ultra-sonic and the obstacles faces it or what you may want.
it uses a led connected to an arduino uno board,as the obstacles be in range the led will light up.
Prepare Your Connections
Set Your Code to the Arduino Board
// Define pins for ultrasonic and buzzer
int const trigPin = 10; int const echoPin = 9;
int const ledpin = 13;
void setup() {
pinMode(trigPin, OUTPUT);
// trig pin will have pulses output
pinMode(echoPin, INPUT);
// echo pin should be input to get pulse width
pinMode(ledpin, OUTPUT); // buzz pin is output to control buzzering }
void loop() { // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet) distance = (duration/2) / 29.1;
// if distance less than 0.25 meter and more than 0 (0 or less means over range)
if (distance <= 25 && distance >= 0) {
// ledup
digitalWrite(ledpin, HIGH);
} else {
// Don't ledup
digitalWrite(ledpin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}
Have Fun :D
adjust the condition in the code to modify your distance alert.
have fun.