#include <Servo.h>
Servo myServo;
#define ECHO_Pin =5;
#define TRIG_Pin =6;
int sudut = 0;
void setup() {
// put your setup code here, to run once:
myServo.attach(servoPin);
pinMode(TRIG_Pin, OUTPUT);
pinMode(ECHO_Pin, INPUT);
}
float jarak_mm()
{
digitalWrite(TRIG_Pin, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_Pin, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_Pin, LOW);
int duration = pulseIn(ECHO_Pin, HIGH)
return duration *0.034 / 2;
}
void loop() {
// put your main code here, to run repeatedly:
float cm = jarak_cm();
if (cm <= 100)
{sudut = 180;
myServo.write(sudut);
delay(1000);}
else
{sudut = 0;
myServo.write(sudut);
delay(1000);}
}