Skip to content

Instantly share code, notes, and snippets.

@RishabhR-Source
Created August 7, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RishabhR-Source/7832686572ba0752d740e3dd4c2d53e6 to your computer and use it in GitHub Desktop.
Save RishabhR-Source/7832686572ba0752d740e3dd4c2d53e6 to your computer and use it in GitHub Desktop.
#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