#define LED1 2
#define LED2 5
#define LED3 18
#define LED4 19
#define echopin 23
#define trigpin 22
long duration;
float jarak;
void setup() {
Serial.begin(9600);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(echopin, INPUT);
pinMode(trigpin, OUTPUT);
}
void loop() {
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration = pulseIn(echopin, HIGH);
jarak = duration*0.034/2;
Serial.print(jarak);
Serial.println(" cm");
if(jarak < 100 ){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
}
else if(jarak > 100 && jarak < 200){
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
}
else if(jarak > 200 && jarak < 300){
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);
}
else if(jarak > 300){
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,HIGH);
}
delay(500);
}