// Include the necessary libraries
#include <NewPing.h>
// Define the pins for the sensor
#define TRIGGER_PIN 4
#define ECHO_PIN 3
// Define the maximum distance we want to sense (in centimeters)
#define MAX_DISTANCE 500
// Initialize the NewPing library with the sensor pins and maximum distance
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Send a ping and get the result (in centimeters)
unsigned int distance = sonar.ping_cm();
// Print the distance to the Serial Monitor
Serial.println(distance);
// Delay for a short amount of time before taking the next measurement
delay(250);
}