-
-
Save RishabhR-Source/7832686572ba0752d740e3dd4c2d53e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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