const int PIN_TO_SENSOR = 19; // the pin that OUTPUT pin of sensor is connected to int pinStateCurrent = LOW; // current state of pin int pinStatePrevious = LOW; // previous state of pin unsigned long lastMeasurementTime = 0; // time of the last measurement // Variables for 10-minute averaging unsigned int motionCount = 0; unsigned int measurementCounter = 0; unsigned int measurement[144]; unsigned int averagePeriod = 10 * 60 * 1000; // 10 minutes in milliseconds unsigned int highestMotionCount = 0; unsigned int lowestMotionCount = 0; unsigned long totalMotionCount = 0; // Accumulated motion count for all periods unsigned int totalMeasurementCounter = 0; // Total number of 10-minute periods void printDiagnostics(unsigned int highestMotionCount, unsigned int lowestMotionCount, float averageMotionCount) { // Print the highest and lowest motion counts Serial.print("Highest motion count: "); Serial.println(highestMotionCount); Serial.print("Lowest motion count: "); Serial.println(lowestMotionCount); // Print the overall average motion count Serial.print("Overall average motion count: "); Serial.println(averageMotionCount); // Print all of the measurements for(int i = 0; i < 144; i++) { Serial.println(measurement[i]); } } void setup() { Serial.begin(9600); pinMode(PIN_TO_SENSOR, INPUT); lastMeasurementTime = millis(); // Set the last measurement time to the current time } void loop() { // Allow for calibration of sensor delay(60000); unsigned long currentTime = millis(); // Check if one second has elapsed since the last measurement if (currentTime - lastMeasurementTime >= 1000) { lastMeasurementTime = currentTime; // Update the last measurement time pinStatePrevious = pinStateCurrent; pinStateCurrent = digitalRead(PIN_TO_SENSOR); if (pinStatePrevious == LOW && pinStateCurrent == HIGH) { Serial.println("Motion detected!"); motionCount++; } else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { Serial.println("Motion stopped!"); } } // Check if the average period (10 minutes) has elapsed if (currentTime - lastMeasurementTime >= averagePeriod) { // Check if the current motion count is the highest if (motionCount > highestMotionCount) { highestMotionCount = motionCount; } // Check if the current motion count is the lowest if (measurementCounter == 1 || motionCount < lowestMotionCount) { lowestMotionCount = motionCount; } // Accumulate the motion count for all periods totalMotionCount += motionCount; // Update the array of measurements measurement[measurementCounter] = motionCount; // Reset the variables for the next 10-minute period lastMeasurementTime = currentTime; motionCount = 0; measurementCounter++; } // Stop taking measurements after 24 hours (86400 seconds = 24 hours * 60 minutes * 60 seconds) if (currentTime >= 86400000) { // Calculate the average motion count for all periods float averageMotionCount = totalMotionCount / (float)totalMeasurementCounter; // Print the averages, the highest motion count, as well as the lowest motion count printDiagnostics(highestMotionCount, lowestMotionCount, averageMotionCount); // Stop the program while (true) { // Do nothing } } }