#include<Servo.h> | |
#define echoPin 4 | |
#define trigPin 5 | |
#define buzzPin 2 | |
Servo Myservo; | |
int pos; | |
int long duration; | |
int distance; | |
void setup() { | |
Myservo.attach(3); | |
pinMode(echoPin, INPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(buzzPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration * 0.034 / 2); | |
if (distance <= 25) { | |
for (pos = 180; pos >= 0; pos--) { | |
Myservo.write(pos); | |
delay(111.1); | |
Serial.println(pos); | |
delay(20); | |
if (pos == 0) { | |
digitalWrite(buzzPin, HIGH); | |
Serial.println("buzzer on"); | |
delay(2000); | |
digitalWrite(buzzPin, LOW); | |
} | |
} | |
} | |
else { | |
Myservo.write(180); | |
} | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment