Introduction: Sweeping Obstacle Detector

The objective of this instructable is to make a setup in which a Ultrasonic sensor is mounted on a servo motor.
I've considered a micro servo (90g), and a red LED.
The connections need to be made as seen in the Fritzig diagram.
Attaching the Ultrasonic sensor and the servo arm should be done using double sided tape and insulation tape.
(Do not consider the joystick, i forgot to remove it)

Parts used:
1) Breadboard
2) Arduino UNO Board
3) Red LED
4) Ultrasound Sensor
5) Tower Pro (9grams) servo
6) Piezoelectric buzzer
7) 220 ohm resistor

The code is as follows:

#define trigPin 7
#define echoPin 6
#define led 13
#define buzzer 11
#include

int ServoHorizontalPin = 3;
int HorizontalPotPin = A0;
int HorizontalPotValue = 0;
int ServoH_Min = 0;
int ServoH_Max = 180;
int pos = 0;
int sound;

Servo HorizontalServo;
int HorizontalServoPosition;

void setup()
{
HorizontalServo.attach(ServoHorizontalPin);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

void loop()
{
for (pos = 0; pos <= 180; pos += 2)
{
alarm();
}
for (pos = 180; pos >= 0; pos -= 2)
{
alarm();
}
}

void alarm()
{
HorizontalServo.write(pos);
delay(25);
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 20)
{
digitalWrite(led, HIGH);
analogWrite(11, 20);
}

else
{
digitalWrite(led,LOW);
analogWrite(11, 0);
}
}

Please do try and give me feedback.
Any improvisations are happily accepted.

Sensors Contest 2016

Participated in the
Sensors Contest 2016