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 unsigned long startTime; //start time unsigned long lastPrintTime; // last print time unsigned long totalMeasurements; // total measurements float totalValue; // total value float highestValue; // highest value float lowestValue; // lowest value //sensor fuct float readSensorValue() { return random (0, 100); } //average function void printSummary() { unsigned long elapsedTime = millis() - startTime; unsigned long totalMinutes = elapsedTime / 60000; //pint average float overallAverage = totalValue / totalMeasurements; Serial.print("Overall Average: "); Serial.println(overallAverage); //print high and low values Serial.print("Highest Value "); Serial.println(highestValue); Serial.print("Lowest Value: "); Serial.println(lowestValue); //print every 10 mins 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 //Variables startTime = millis(); lastPrintTime = startTime; totalMeasurements = 0; totalValue = 0; highestValue = -INFINITY; lowestValue = INFINITY; } void loop() { //measurements unsigned long currentTime = millis(); unsigned long Interval = 1000; //define the interval in milliseconds if (currentTime - lastPrintTime >= Interval) { //read sensor value float value = readSensorValue(); //update all measurements totalValue += value; totalMeasurements++; //update highest and lowest values if (value > highestValue) { highestValue = value; } if (value < lowestValue) { lowestValue = value; } // summary printSummary(); //update print time lastPrintTime = currentTime; } }