Skip to content

Instantly share code, notes, and snippets.

@detik19
Created December 22, 2018 09:09
Show Gist options
  • Save detik19/920b5780dd779fef895b41af5c95a0ba to your computer and use it in GitHub Desktop.
Save detik19/920b5780dd779fef895b41af5c95a0ba to your computer and use it in GitHub Desktop.
#define trigPin 11
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2); a
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
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