Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#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