const int windowSize = 1000; // Size of the moving average window
int readings[windowSize]; // Array to store previous readings
int index = 0; // Index for the next reading
int total = 0; // Total of the readings
void setup() {
Serial.begin(9600); // Initialize serial communication
for (int i = 0; i < windowSize; i++) {
readings[i] = 0; // Initialize readings array
}
}
void loop() {
// Read the analog input from A0
int sensorValue = analogRead(A0);
// Subtract the oldest reading from the total
total -= readings[index];
// Store the new reading in the array
readings[index] = sensorValue;
// Add the new reading to the total
total += sensorValue;
// Increment the index, wrap around if necessary
index = (index + 1) % windowSize;
// Calculate the moving average
float movingAverage = (float)total / windowSize;
// Print the moving average to the serial monitor
Serial.print("Moving Average: ");
Serial.println(movingAverage);
// Delay for stability
delay(100);
}