Last active
January 22, 2019 22:41
-
-
Save ColeFrench/8845fa11f29b64e2887e19b9b0a054ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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