Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ColeFrench
Last active January 22, 2019 22:41
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 ColeFrench/8845fa11f29b64e2887e19b9b0a054ed to your computer and use it in GitHub Desktop.
Save ColeFrench/8845fa11f29b64e2887e19b9b0a054ed to your computer and use it in GitHub Desktop.
/**
* The digital pin that receives input from the ultrasonic sensor.
*/
const int echoPin = 2;
/**
* The digital pin that sends output to the ultrasonic sensor.
*/
const int trigPin = 4;
void setup() {
pinMode(echoPin, INPUT); // Register echoPin for receiving input
pinMode(trigPin, OUTPUT); // Register trigPin for sending output
Serial.begin(9600); // Begin serial communication to receive data from the ultrasonic sensor
}
void loop() {
// Send a short low pulse to ensure a clean high one.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a ten-second high pulse.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the high pulse and print out its duration.
const long duration = pulseIn(echoPin, HIGH);
Serial.print("Period (microseconds): ");
Serial.println(duration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment