#define trigpin 8
#define echopin 7
#define R1 9
#define Y1 10
#define G1 6
#define W1 5
#define S1 11
#define S2 1
#define P1 4
long durasi;
int jarak;



void setup() {
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(R1, OUTPUT);
pinMode(Y1, OUTPUT);
pinMode(G1, OUTPUT);
pinMode(W1, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(P1, OUTPUT);
Serial.begin(96000);
Serial.println("Belajar Sensor");
Serial.println("Dengan Sensor UltraSonik");



}

void loop() {
  digitalWrite(trigpin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigpin, HIGH);
  delayMicroseconds(10);

 durasi = pulseIn(echopin, HIGH);
 jarak = durasi * 0.034/2;

if (jarak > 320){
  digitalWrite(R1, HIGH);
  digitalWrite(Y1, LOW);
  digitalWrite(G1, LOW);
  digitalWrite(W1, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(P1, LOW);
  tone(S2,3000);
} else if(jarak > 270) {
  digitalWrite(R1, LOW);
  digitalWrite(Y1, HIGH);
  digitalWrite(G1, LOW); 
  digitalWrite(W1, LOW); 
  digitalWrite(S1, LOW);
  digitalWrite(P1, LOW);
  tone(S2 ,2700);
} else if (jarak > 150){
  digitalWrite(R1, LOW);
  digitalWrite(Y1, LOW);
  digitalWrite(G1, HIGH); 
  digitalWrite(W1, LOW); 
  digitalWrite(S1, LOW); 
  digitalWrite(P1, LOW);
  tone(S2, 1700);
}
else if (jarak > 110){
  digitalWrite(W1, HIGH);
  digitalWrite(G1, LOW);
  digitalWrite(Y1, LOW);
  digitalWrite(R1, LOW);
  digitalWrite(S1, LOW);
  digitalWrite(P1, LOW);
  tone(S2, 1200);
}
else if (jarak > 70){
digitalWrite(S1,HIGH);
digitalWrite(R1, LOW);
digitalWrite(G1, LOW);
digitalWrite(Y1, LOW);
digitalWrite(W1, LOW);
tone(S2, 700);
}
else {
  digitalWrite(P1, HIGH);
  digitalWrite(R1, LOW);
digitalWrite(Y1, LOW);
digitalWrite(G1, LOW);
digitalWrite(W1, LOW);
digitalWrite(S1, LOW);
tone(S2,200);
}



Serial.println(" cm");
 Serial.print(jarak);
}