2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
Smoothing
|
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
Reads repeatedly from an analog input, calculating a running average and
|
|
|
|
printing it to the computer. Keeps ten readings in an array and continually
|
|
|
|
averages them.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
The circuit:
|
2017-07-12 22:18:23 +02:00
|
|
|
- analog sensor (potentiometer will do) attached to analog input 0
|
2011-08-30 21:33:32 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 22 Apr 2007
|
|
|
|
by David A. Mellis <dam@mellis.org>
|
2012-04-09 16:48:11 +02:00
|
|
|
modified 9 Apr 2012
|
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
This example code is in the public domain.
|
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/Smoothing
|
2011-08-30 21:33:32 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
// Define the number of samples to keep track of. The higher the number, the
|
|
|
|
// more the readings will be smoothed, but the slower the output will respond to
|
|
|
|
// the input. Using a constant rather than a normal variable lets us use this
|
|
|
|
// value to determine the size of the readings array.
|
2011-08-30 21:33:32 +02:00
|
|
|
const int numReadings = 10;
|
|
|
|
|
|
|
|
int readings[numReadings]; // the readings from the analog input
|
2014-07-02 15:44:26 +02:00
|
|
|
int readIndex = 0; // the index of the current reading
|
2011-08-30 21:33:32 +02:00
|
|
|
int total = 0; // the running total
|
|
|
|
int average = 0; // the average
|
|
|
|
|
|
|
|
int inputPin = A0;
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2011-08-30 21:33:32 +02:00
|
|
|
// initialize serial communication with computer:
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.begin(9600);
|
|
|
|
// initialize all the readings to 0:
|
2015-07-06 15:18:33 +02:00
|
|
|
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
|
2013-10-21 09:58:40 +02:00
|
|
|
readings[thisReading] = 0;
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// subtract the last reading:
|
2014-07-02 16:05:23 +02:00
|
|
|
total = total - readings[readIndex];
|
2013-10-21 09:58:40 +02:00
|
|
|
// read from the sensor:
|
2014-07-02 16:05:23 +02:00
|
|
|
readings[readIndex] = analogRead(inputPin);
|
2011-08-30 21:33:32 +02:00
|
|
|
// add the reading to the total:
|
2014-07-02 16:05:23 +02:00
|
|
|
total = total + readings[readIndex];
|
2013-10-21 09:58:40 +02:00
|
|
|
// advance to the next position in the array:
|
2014-07-02 16:05:23 +02:00
|
|
|
readIndex = readIndex + 1;
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
// if we're at the end of the array...
|
2015-07-06 15:18:33 +02:00
|
|
|
if (readIndex >= numReadings) {
|
2013-10-21 09:58:40 +02:00
|
|
|
// ...wrap around to the beginning:
|
2014-07-02 16:05:23 +02:00
|
|
|
readIndex = 0;
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2011-08-30 21:33:32 +02:00
|
|
|
|
|
|
|
// calculate the average:
|
2013-10-21 09:58:40 +02:00
|
|
|
average = total / numReadings;
|
2011-08-30 21:33:32 +02:00
|
|
|
// send it to the computer as ASCII digits
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println(average);
|
|
|
|
delay(1); // delay in between reads for stability
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|