#include <Servo.h>
#define trigPin 3
#define echoPin 2
#define pirPin 1
const int thresholdValue = 800;
Servo servo;
int sound = 250;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
servo.attach(4);
}
int pos = 0;
void loop() {
for (pos = 0; pos <= 180; pos += 1) {
servo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo.write(pos);
delay(15);
}
int irValue = analogRead(pirPin);
// if (irValue < thresholdValue) {
// }
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 20) {
Serial.println("The distance is less than 20");
servo.write(90);
}
else {
servo.write(0);
}
if (distance > 20 || distance <= 400){
Serial.println("The distance is more than 20");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}