Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int numReadings = 10; // Total number of readings to use for avaraging / smoothing
- int readings; // the readings from the analog input
- int readIndex = 0; // the index of the current reading
- int total = 0; // the running total
- int average = 0; // the average
- int inputPin = A0;
- void setup() {
- Serial.begin(9600); // initialize serial communication with computer:
- for (int thisReading = 0; thisReading < numReadings; thisReading++) {
- readings = 0; // initialize all the readings to 0:
- }
- }
- void loop() {
- total = total - readings; // subtract the last reading:
- readings = analogRead(inputPin); // read the desired data from your sensor here...
- total = total + readings; // add the reading to the total:
- readIndex = readIndex + 1; // advance to the next position in the array:
- if (readIndex >= numReadings) { // if we're at the end of the array...
- readIndex = 0; // ...wrap around to the beginning:
- }
- average = total / numReadings; // calculate the average:
- Serial.println(average); // Print to serial monitor
- delay(1); // delay in between reads for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement