#define ECHO_PIN 5

#define TRIG_PIN 4

#define ledMerah 8

#define ledKuning 7

#define ledHijau 6

#define ledOrange 12

#define ledUngu 13

#define ledBiru 10 

#define ledHijauMuda 2 

void setup() {

Serial.begin(115200);

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT); pinMode(ledMerah, OUTPUT);

pinMode(ledKuning, OUTPUT);

pinMode(ledHijau, OUTPUT);

pinMode(ledOrange, OUTPUT);

pinMode(ledUngu, OUTPUT);

pinMode(ledBiru, OUTPUT);

pinMode(ledHijauMuda, OUTPUT);


}

float readDistanceCM() {

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() {

float distance = readDistanceCM();

Serial.print("Measured distance: "); Serial.println(readDistanceCM());

if(distance <= 50){

digitalWrite(ledMerah, HIGH); digitalWrite(ledKuning, LOW);

digitalWrite(ledHijau, LOW);

} else if(distance <= 100){

digitalWrite(ledMerah, LOW);

digitalWrite(ledKuning, HIGH); 
digitalWrite(ledHijau, LOW);

} else if(distance <= 150){
  digitalWrite(ledOrange, HIGH);
  digitalWrite(ledUngu, LOW);
  digitalWrite(ledBiru, LOW);
  digitalWrite(ledHijauMuda, LOW);

}else if(distance <= 200){
  digitalWrite(ledOrange, LOW);
  digitalWrite(ledUngu, HIGH);
  digitalWrite(ledBiru, LOW);
  digitalWrite(ledHijauMuda, LOW);
  
}else if(distance <= 250){
  digitalWrite(ledOrange, LOW);
  digitalWrite(ledUngu, LOW);
  digitalWrite(ledBiru, HIGH);
  digitalWrite(ledHijauMuda, LOW);
  
}else if(distance <= 300){
  digitalWrite(ledOrange, LOW);
  digitalWrite(ledUngu, LOW);
  digitalWrite(ledBiru, LOW);
  digitalWrite(ledHijauMuda, HIGH);
  
}


else{

digitalWrite(ledMerah, LOW);

digitalWrite(ledKuning, LOW); digitalWrite(ledHijau, HIGH);

}

delay(100);

}