Skip to content

Instantly share code, notes, and snippets.

@winhtut
Created July 5, 2018 03:03
Show Gist options
  • Save winhtut/bfc00ec277770985c7aec5781ab02234 to your computer and use it in GitHub Desktop.
Save winhtut/bfc00ec277770985c7aec5781ab02234 to your computer and use it in GitHub Desktop.
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("range");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment