#define trg 4
#define echo 5
int pinled = 6;
int pinrelay = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(trg, OUTPUT);
pinMode(echo, INPUT);
pinMode(pinled, OUTPUT);
pinMode(pinrelay, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trg, HIGH);
delay(10);
digitalWrite(trg, LOW);
int del = pulseIn(echo, HIGH);
Serial.print("Jarak Dalam cm = "); //in inch duration/148
Serial.println(del/58);
delay(1000);
if(echo<100){
digitalWrite(pinrelay, HIGH);
delay(100);
}
else{
digitalWrite(pinrelay, LOW);
delay(100);
}
}