int echoPin = 12;
int trigPin = 13;
int ledH = 18;
int ledK = 5;
int ledM = 15;
int ledB = 22;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(ledH, OUTPUT);
pinMode(ledK, OUTPUT);
pinMode(ledM, OUTPUT);
pinMode(ledB, OUTPUT);
Serial.begin(115200);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//int durasipantulan = pulseIn(echoPin,HIGH);
//membuat nilai ultrasonik menjadi hasil perhitungan durasi *0,034/2;
//return durasipantulan *0.034/2;
long durasi = pulseIn(echoPin,HIGH);
long jarak =(durasi/2)/29;
Serial.print(jarak);
Serial.println(" Cm");
if (jarak <50)
{
digitalWrite(ledM, LOW);
digitalWrite(ledH, LOW);
digitalWrite(ledK, LOW);
digitalWrite(ledB, LOW);
}
if (jarak > 50 && jarak <100)
{
digitalWrite(ledM, LOW);
digitalWrite(ledH, HIGH);
digitalWrite(ledK, LOW);
digitalWrite(ledB, LOW);
}
if (jarak >100 && jarak <200)
{
digitalWrite(ledM, LOW);
digitalWrite(ledH, LOW);
digitalWrite(ledK, HIGH);
digitalWrite(ledB, LOW);
}
if (jarak >200 && jarak <300)
{
digitalWrite(ledM, HIGH);
digitalWrite(ledH, LOW);
digitalWrite(ledK, LOW);
digitalWrite(ledB, LOW);
}
if (jarak >300)
{
digitalWrite(ledM, LOW);
digitalWrite(ledH, LOW);
digitalWrite(ledK, LOW);
digitalWrite(ledB, HIGH);
}
}