#include <Servo.h>
Servo servo1;
#define echoPin 3
#define trigPin 4
int maximumRange = 200;
int minimumRange = 0;
long duration, distance;
void setup() {
Serial.begin (9600);
servo1.attach(2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);delayMicroseconds(2);
digitalWrite(trigPin, HIGH);delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
Serial.print(distance);
Serial.println (" cm ");
delay (500);
if (distance < 25)
{
servo1.write(0);
}
else
{
servo1.write(180);
}
}
Tidak ada komentar:
Posting Komentar