Int trigPin=7;
Int echoPin=8;
Int LedPin=5;
Int buzzerpin=3;
Long duration;
Float short;
Servo Servo;
Void setup ()
{
PinMode (trigPin, the OUTPUT);
PinMode (echoPin, INPUT);
PinMode (buzzerpin, the OUTPUT);
PinMode (LedPin, the OUTPUT);
Serial.begin(9600);
Servo. Attach (9);
}
Void loop ()
{short=calculateDistance ();
If (short <=100)
{
DigitalWrite (buzzerpin, HIGH);
DigitalWrite (LedPin, LOW);
}
The else {
DigitalWrite (LedPin, LOW);
DigitalWrite (buzzerpin, LOW);
}
DigitalWrite (buzzerpin, LOW);
For (int I=15; I <=165; I++)
{
Servo. Write (I);
Delay (300);
Short=calculateDistance ();
Serial. Print (I);
Serial. Print (", ");
Serial. Print (short);
Serial. Print (";" );
}
For (int I=165; I>=1; I -)
{
Servo. Write (I);
Delay (300);
Short=calculateDistance ();
Serial. Print (I);
Serial. Print (", ");
Serial. Print (short);
Serial. Print (";" );
}
}
Int calculateDistance ()
{
DigitalWrite (trigPin, LOW);
DelayMicroseconds (2);
DigitalWrite (trigPin, HIGH);
DelayMicroseconds (10);
DigitalWrite (trigPin, LOW);
Duration=pulseIn (echoPin, HIGH);
Short duration of=* 0.034/2;
Return short;
}