// Include the Ultrasonic library
#include <Ultrasonic.h>
// Define the pins for the ultrasonic sensor
#define trigPin 2
#define echoPin 3
// Create an instance of the Ultrasonic class
Ultrasonic ultrasonic(trigPin, echoPin);
// Define a variable to store the distance measured by the sensor
int distance;
void setup() {
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
// Read the distance from the sensor
distance = ultrasonic.read();
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait for 500 milliseconds before taking another reading
delay(500);
}
// Code sourced from: https://www.arduino.cc/en/Tutorial/BuiltInExamples/UltrasonicSensor