Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Contoh program sensor HC-SR04
- created by papermindvention.blogspot.com
- march 2018
- */
- const int trigPin = 2;
- const int echoPin = 3;
- int led= 13;
- int duration = 0; //variable untuk menyimpan hasil inputan echo
- int jarak = 0; //variable untuk menyimpan hasil dari perhitungan jarak
- void setup(){
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(led, OUTPUT);}
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- jarak = (duration*0.034)/2;//rumus
- Serial.print(jarak);
- Serial.println(" cm");
- Serial.println(duration);
- if (jarak<=70) {
- digitalWrite(led, HIGH);}
- else { digitalWrite(led, LOW); }
- }
Advertisement
Add Comment
Please, Sign In to add comment