#define pinTriger 12
#define pinEcho 15
#define pinLED1 27
#define pinLED2 21
#define pinLED3 18
#define pinBuz 19
void setup() {
Serial.begin(112500);
pinMode(pinTriger, OUTPUT);
pinMode(pinEcho, INPUT);
pinMode(pinLED1, OUTPUT);
pinMode(pinLED2, OUTPUT);
pinMode(pinLED3, OUTPUT);
pinMode(pinBuz, OUTPUT);
}
void loop() {
long durasi, jarak;
digitalWrite(pinTriger, LOW);
delayMicroseconds(2);
digitalWrite(pinTriger, HIGH);
delayMicroseconds(10);
digitalWrite(pinEcho, LOW);
durasi=pulseIn(pinEcho, HIGH);
jarak=(durasi/2)/29;
Serial.print("Jarak :");
Serial.print(jarak);
Serial.println(" cm");
if(jarak >= 30){
digitalWrite(pinLED1, HIGH);
digitalWrite(pinLED2, HIGH);
digitalWrite(pinLED3, HIGH);
digitalWrite(pinBuz, HIGH);
}else if (jarak >= 20 && jarak < 30){
digitalWrite(pinLED1, HIGH);
digitalWrite(pinLED2, HIGH);
digitalWrite(pinLED3, HIGH);
digitalWrite(pinBuz, LOW);
}else if (jarak >= 10 && jarak < 20){
digitalWrite(pinLED1, HIGH);
digitalWrite(pinLED2, HIGH);
digitalWrite(pinLED3, LOW);
digitalWrite(pinBuz, LOW);
}else if (jarak >= 5 && jarak < 10){
digitalWrite(pinLED1, HIGH);
digitalWrite(pinLED2, LOW);
digitalWrite(pinLED3, LOW);
digitalWrite(pinBuz, LOW);
}else{
digitalWrite(pinLED1, LOW);
digitalWrite(pinLED2, LOW);
digitalWrite(pinLED3, LOW);
digitalWrite(pinBuz, LOW);
}
delay(1000);
}