Created
January 22, 2019 22:40
-
-
Save ColeFrench/82b65d2d968e1c76eba664b7052985b9 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); | |
// Store the high pulse's duration. | |
const long duration = pulseIn(echoPin, HIGH); | |
// Calculate and print the distance to the target. | |
const double distance = microsecondsToDistance(duration); | |
Serial.print("Distance: "); | |
Serial.println(distance); | |
} | |
/** | |
* @param microseconds a number of microseconds | |
* @return the conversion of the provided microseconds into a distance | |
*/ | |
const double microsecondsToDistance(const long microseconds) { | |
// Initialize m and b to their respective values in the formula, y = mx + b. | |
// y = distance, x = time (in microseconds). | |
const double m = 1.0; | |
const double b = 0.0; | |
return m * microseconds + b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment