Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const int trigger = 2;
const int echo = 5;
int distance;
long duration;
void setup() {
Serial.begin(9600);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trigger,LOW); // Set sinyal trigger menjadi LOW
delayMicroseconds(2); // delay 2 us
digitalWrite(trigger,HIGH); // Set sinyal trigger menjadi HIGH
delayMicroseconds(10); // delay 10 us
digitalWrite(trigger,LOW); // Set sinyal trigger LOW kembali
duration = pulseIn(echo,HIGH); // Menghitung waktu saat pin echo HIGH
distance = duration/58.8; // Rumus untuk menghitung jarak
Serial.print("Distance : ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment