Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/03b12d18b0eb7b1e207b2fee3bdc52c9 to your computer and use it in GitHub Desktop.
Save anonymous/03b12d18b0eb7b1e207b2fee3bdc52c9 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
int trigPin = 5;
int echoPin = 4;
long duration, cm;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.println("cm");
delay(250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment