const int TRIGGER_PIN = 16; // Ultrasonic sensor trigger pin const int ECHO_PIN = 17; // Ultrasonic sensor echo pin const int PHOTO_PIN = 34; // Photoresistor analog pin const unsigned long DELAY_TIME = 1000; const unsigned long AVERAGE_PERIOD = 600000; unsigned long startTime = 0; unsigned long lastAverageTime = 0; unsigned long lastMeasurementTime = 0; unsigned int measurementCount = 0; float totalDistance = 0; unsigned int totalPhotoValue = 0; float highestDistance = 0; unsigned int highestPhotoValue = 0; float lowestDistance = 0; unsigned int lowestPhotoValue = 0; void setup() { Serial.begin(115200); startTime = millis(); lastAverageTime = startTime; } float measureDistance() { pinMode(TRIGGER_PIN, OUTPUT); digitalWrite(TRIGGER_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIGGER_PIN, LOW); pinMode(ECHO_PIN, INPUT); unsigned long duration = pulseIn(ECHO_PIN, HIGH); float distance = (duration / 2.0) * 0.0343; // Calculate distance in centimeters return distance; } int measurePhotoresistor() { int photoValue = analogRead(PHOTO_PIN); return photoValue; } void updateStatistics(float distance, unsigned int photoValue) { /* Update highest distance */ if (distance > highestDistance) { highestDistance = distance; } /* Update highest photo value */ if (photoValue > highestPhotoValue) { highestPhotoValue = photoValue; } /* Update lowest distance */ if (measurementCount == 0 || distance < lowestDistance) { lowestDistance = distance; } /* Update lowest photo value */ if (measurementCount == 0 || photoValue < lowestPhotoValue) { lowestPhotoValue = photoValue; Serial.println("test 1"); } } void printAverages(float averageDistance, unsigned int averagePhotoValue) { /* Print the overall average */ Serial.print("Overall Average: Distance="); Serial.print(averageDistance); Serial.print(", PhotoValue="); Serial.println(averagePhotoValue); /* Print the highest and lowest values */ Serial.print("Highest Values: Distance="); Serial.print(highestDistance); Serial.print(", PhotoValue="); Serial.println(highestPhotoValue); Serial.print("Lowest Values: Distance="); Serial.print(lowestDistance); Serial.print(", PhotoValue="); Serial.println(lowestPhotoValue); } void loop() { unsigned long currentTime = millis(); if (currentTime - lastMeasurementTime >= DELAY_TIME) { /* Read distance from the ultrasonic sensor */ float distance = measureDistance(); /* Read value from the photoresistor */ int photoValue = measurePhotoresistor(); /* Update cumulative totals and statistics */ totalDistance += distance; totalPhotoValue += photoValue; measurementCount++; updateStatistics(distance, photoValue); lastMeasurementTime = currentTime; } if (currentTime - lastAverageTime >= AVERAGE_PERIOD) { /* Calculate average for the last 10 minutes */ float averageDistance = totalDistance / measurementCount; unsigned int averagePhotoValue = totalPhotoValue / measurementCount; /* Print the averages */ printAverages(averageDistance, averagePhotoValue); /* Reset the variables */ totalDistance = 0; totalPhotoValue = 0; measurementCount = 0; lastAverageTime = currentTime; } }