const int trigPin = 5; // Pin number for the trigger pin of the ultrasound sensor const int echoPin = 18; // Pin number for the echo pin of the ultrasound sensor #define SOUND_SPEED 0.0343 // Speed of sound in cm/µs long duration; // Variable to store the duration of the sound wave travel float distanceCm; // Variable to store the calculated distance in centimeters const int numMeasurements = 600; // Number of measurements to store (10 minutes with measurements every second) float measurements[numMeasurements]; // Array to store the measurements int currentMeasurement = 0; // Index of the current measurement int totalMeasurements = 0; // Total number of measurements taken unsigned long previousTime = 0; // Variable to store the previous time for summary update unsigned long summaryInterval = 10000; // Interval for summary update in milliseconds (10 seconds) float overallAverage = 0; // Variable to store the overall average distance float highestValue = 0; // Variable to store the highest measured distance float lowestValue = 0; // Variable to store the lowest measured distance float sum = 0; // Variable to store the sum of measurements void setup() { Serial.begin(115200); // Initialize serial communication with a baud rate of 115200 pinMode(trigPin, OUTPUT); // Set the trigger pin as an output pinMode(echoPin, INPUT); // Set the echo pin as an input Serial.println("Starting Program"); // Print a message to indicate the program has started } void loop() { Serial.println(distanceCm); // Print the current distance in centimeters delay(1000); // Delay for 1 second if (Serial.available() > 0) { // Check if there is any input from the serial monitor char input = Serial.read(); // Read the input character if (input == 's') { // If the input is 's' printSummary(); // Call the function to print the summary } } unsigned long currentTime = millis(); // Get the current time in milliseconds if (currentTime - previousTime >= summaryInterval) { // If the time since the last summary update is greater than or equal to the summary interval updateSummary(); // Call the function to update the summary previousTime = currentTime; // Update the previous time } digitalWrite(trigPin, LOW); // Set the trigger pin to low delayMicroseconds(2); // Delay for 2 microseconds digitalWrite(trigPin, HIGH); // Set the trigger pin to high delayMicroseconds(10); // Delay for 10 microseconds digitalWrite(trigPin, LOW); // Set the trigger pin to low duration = pulseIn(echoPin, HIGH); // Measure the duration of the echo pulse distanceCm = duration * SOUND_SPEED / 2; // Calculate the distance in centimeters based on the duration measurements[currentMeasurement] = distanceCm; // Store the current measurement in the measurements array currentMeasurement = (currentMeasurement + 1) % numMeasurements; // Update the current measurement index (wrap around to the beginning if it reaches the end) totalMeasurements = min(totalMeasurements + 1, numMeasurements); // Update the total measurements count, but ensure it doesn't exceed the maximum number of measurements delay(100); // Delay for 100 milliseconds } void printSummary() { highestValue = measurements[0]; // Initialize the highest value with the first measurement lowestValue = measurements[0]; // Initialize the lowest value with the first measurement sum = 0; // Reset the sum before calculating for (int i = 0; i < totalMeasurements; i++) { // Loop through the stored measurements sum += measurements[i]; // Add the current measurement to the sum if (measurements[i] > highestValue) { // If the current measurement is higher than the highest value highestValue = measurements[i]; // Update the highest value } if (measurements[i] < lowestValue) { // If the current measurement is lower than the lowest value lowestValue = measurements[i]; // Update the lowest value } if ((i + 1) % 10 == 0) { // If 10 measurements have been processed float average = sum / 10; // Calculate the average of the 10 measurements Serial.print("Average after "); Serial.print(i + 1); Serial.print(" seconds: "); Serial.println(average); // Print the average sum = 0; // Reset the sum for the next set of measurements } } printOverallAverage(); // Call the function to print the overall average Serial.print("Highest Value: "); Serial.println(highestValue); // Print the highest value Serial.print("Lowest Value: "); Serial.println(lowestValue); // Print the lowest value } void updateSummary() { highestValue = measurements[0]; // Initialize the highest value with the first measurement lowestValue = measurements[0]; // Initialize the lowest value with the first measurement sum = 0; // Reset the sum before calculating for (int i = 0; i < totalMeasurements; i++) { // Loop through the stored measurements sum += measurements[i]; // Add the current measurement to the sum if (measurements[i] > highestValue) { // If the current measurement is higher than the highest value highestValue = measurements[i]; // Update the highest value } if (measurements[i] < lowestValue) { // If the current measurement is lower than the lowest value lowestValue = measurements[i]; // Update the lowest value } } overallAverage = sum / totalMeasurements; // Calculate the overall average by dividing the sum by the total number of measurements } void printOverallAverage() { float totalSum = 0; for (int i = 0; i < totalMeasurements; i++) { // Loop through the stored measurements totalSum += measurements[i]; // Add the current measurement to the total sum } overallAverage = totalSum / totalMeasurements; // Calculate the overall average by dividing the total sum by the total number of measurements Serial.print("Overall Average: "); Serial.println(overallAverage); // Print the overall average }