#include <Servo.h>
Servo myservo; //penyimpanan data
int pos = 0;
const int trigPin = 2;
const int echoPin = 3;
long waktu; //bukan cuma menyimpan waktu. dapat menampung data 32bit
int jarak;
void setup() {
myservo.attach(8); //sinyal data - pin 3
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); //starting
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
waktu = pulseIn(echoPin, HIGH);
jarak = waktu/58.8; //58=defultnya sensor
if (jarak < 10)
{
myservo.write(180);
}
else
{
myservo.write(50);
}
}