#include <Servo.h>
Servo serboo;
int sirbo = 9;
int trig = 6;
int echo = 5;
int bzzz = 3;
long duration, distance;
void setup() {
// put your setup code here, to run once:
serboo.attach(sirbo);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(bzzz, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (trig, LOW);
delayMicroseconds(2);
digitalWrite (trig, HIGH);
delayMicroseconds(10);
duration = pulseIn (echo, HIGH);
distance = duration*0.034/2;
Serial.print("distance: ");
Serial.print(distance);
Serial.println("cm");
if (distance>100) {
noTone(bzzz);
serboo.write(0);
}
else {
tone(bzzz, 1000);
serboo.write(120);
}
}