const int trigPin = 2;
const int echoPin = 15;
const int greenLedPin = 18;
const int yellowLedPin = 5;
const int redLedPin = 4;

void setup() {
  Serial.begin(9600);
    pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
        pinMode(greenLedPin, OUTPUT);
          pinMode(yellowLedPin, OUTPUT);
            pinMode(redLedPin, OUTPUT);
            }

            void loop() {
              digitalWrite(trigPin, LOW);
                delayMicroseconds(2);
                  
                    digitalWrite(trigPin, HIGH);
                      delayMicroseconds(10);
                        digitalWrite(trigPin, LOW);
                          
                            long duration = pulseIn(echoPin, HIGH);
                              float distance = (duration * 0.0343) / 2;  // Kecepatan suara adalah 343 meter/detik

                                Serial.print("Jarak: ");
                                  Serial.print(distance);
                                    Serial.println(" cm");

                                      if (distance > 150) {
                                          digitalWrite(redLedPin, HIGH);
                                              digitalWrite(yellowLedPin, LOW);
                                                  digitalWrite(greenLedPin, LOW);
                                                    } else if (distance > 100) {
                                                        digitalWrite(yellowLedPin, HIGH);
                                                            digitalWrite(redLedPin, LOW);
                                                                digitalWrite(greenLedPin, LOW);
                                                                  } else if (distance > 50) {
                                                                      digitalWrite(greenLedPin, HIGH);
                                                                          digitalWrite(redLedPin, LOW);
                                                                              digitalWrite(yellowLedPin, LOW);
                                                                                } else {
                                                                                    digitalWrite(redLedPin, LOW);
                                                                                        digitalWrite(yellowLedPin, LOW);
                                                                                            digitalWrite(greenLedPin, LOW);
                                                                                              }

                                                                                                delay(1000);  // Jeda satu detik sebelum pengukuran berikutnya
                                                                                                }