const int trigPin = 5; const int echoPin = 18; //define sound speed in cm/uS #define SOUND_SPEED 0.034 #define CM_TO_INCH 0.393701 long duration; float distanceCm; float distanceInch; //Constants const unsigned long INTERVAL = 6000000; // Interval of 10 minuetes in milliseconds //Variables unsigned long startTime; // Start time of the program unsigned long lastPrintTime; //Time of the last summary print unsigned long totalMeasurements; //Total numnber of measurements float totalValue; // Accumulated total value of measurements float highestValue; // Highest measured value float lowestValue; // Lowest measured value //Function to read sensor value (example function) float readSensorValue() { // Replace with your sensor reading logic // This is just a placeholder return random (0,144); } // Function to print the summary void printSummary() { unsigned long elapsedTime = millis() - startTime; unsigned long totalMinutes = elapsedTime / 60000; // Print overall average float overallAverage = totalValue / totalMeasurements; Serial.print("Overall Average:"); Serial.println(overallAverage); //Print highest and lowest values Serial.print("Highest Value "); Serial.println(highestValue); Serial.print("Lowest Value: "); Serial.println(lowestValue); //Print 10-minute averages Serial.println("10-minute Averages:"); for (unsigned long i=0; i< totalMinutes; i += 10) { float average = totalValue / totalMeasurements; Serial.print("Minute "); Serial.print(i); Serial.print(" Average: "); Serial.println(average); } } void setup() { Serial.begin(115200); // Starts the serial communication pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input //Initialize variables startTime = millis(); lastPrintTime = startTime; totalMeasurements = 0; totalValue = 0; highestValue = INFINITY; lowestValue = INFINITY; } void loop(){ //Check if it's time to take a measurement unsigned long currentTime = millis(); if (currentTime - lastPrintTime >= INTERVAL) { // Read sensor value float value = readSensorValue(); // Update total value and number of measurements totalValue += value; totalMeasurements++; // Update highest and lowest values if (value > highestValue) { highestValue = value; } if (value < lowestValue) { lowestValue = value; } // Print summary printSummary(); //Update last print time lastPrintTime = currentTime; } // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculate the distance distanceCm = duration * SOUND_SPEED/2; // Convert to inches distanceInch = distanceCm * CM_TO_INCH; // Prints the distance in the Serial Monitor Serial.print("Distance (cm): "); Serial.println(distanceCm); Serial.print("Distance (inch): "); Serial.println(distanceInch); delay(1000); }