Tutorial.Smoothing History

Hide minor edits - Show changes to markup

May 02, 2012, at 03:41 AM by Scott Fitzgerald -
Changed line 35 from:
to:
November 16, 2011, at 04:12 AM by Scott Fitzgerald -
Changed line 35 from:
to:
September 30, 2011, at 03:11 AM by Scott Fitzgerald -
Changed line 35 from:
to:
September 23, 2010, at 09:42 PM by Christian Cerrito -
Changed line 44 from:
to:
September 23, 2010, at 09:42 PM by Christian Cerrito -
Changed lines 3-4 from:

Smoothing

to:

Smoothing

Changed lines 24-26 from:

to:

September 23, 2010, at 09:37 PM by Christian Cerrito -
Added line 44:
September 17, 2010, at 06:23 PM by Tom Igoe -
Changed lines 5-6 from:

Reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is exceptionally useful for smoothing out the values from jumpy/erratic sensors, and also demonstrates the use of arrays to store data.

to:

This sketch reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is useful for smoothing out the values from jumpy or erratic sensors, and also demonstrates the use of arrays to store data.

Changed lines 29-32 from:

The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

Altering the size of the array used, by changing const int numReadings = 10, to a larger value will smooth the data collected even further.

to:

The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each new value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

Altering the size of the array used, by changing numReadings to a larger value will smooth the data collected even further.

Changed line 47 from:
  • AnalogInOutSerial - read an analog pin, map the result, and use that data to dim or brighten an LED
to:
  • AnalogInOutSerial - read an analog pin, map the result, and use that data to dim or brighten an LED
September 16, 2010, at 10:09 PM by Tom Igoe -
Changed line 35 from:
to:
August 26, 2010, at 09:05 AM by Christian Cerrito -
Deleted line 48:
  • Smoothing - smooth multiple readings of an analog input
August 26, 2010, at 09:04 AM by Christian Cerrito -
Changed lines 44-45 from:
  • [[Reference/serial]()
to:
August 26, 2010, at 09:03 AM by Christian Cerrito -
Changed lines 31-32 from:

Changing the size of the array, from holding 10 values to, say, 20 will smooth your data even further.

to:

Altering the size of the array used, by changing const int numReadings = 10, to a larger value will smooth the data collected even further.

Changed lines 37-51 from:
to:
August 26, 2010, at 08:59 AM by Christian Cerrito -
Changed lines 29-30 from:

The code below sequentially stores 10 readings from your analog sensor into an array, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

to:

The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

August 26, 2010, at 08:53 AM by Christian Cerrito -
Changed lines 29-30 from:

The code below sequentially stores 10 readings from your analog sensor into an array, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smoothing outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

to:

The code below sequentially stores 10 readings from your analog sensor into an array, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

August 26, 2010, at 08:52 AM by Christian Cerrito -
Added lines 28-32:

The code below sequentially stores 10 readings from your analog sensor into an array, one by one. With each advancing value, the sum of all the numbers is generated and divided, producing an average value which then be used to smoothing outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

Changing the size of the array, from holding 10 values to, say, 20 will smooth your data even further.

August 26, 2010, at 08:46 AM by Christian Cerrito -
Changed line 7 from:

'Hardware

to:

Hardware

August 26, 2010, at 08:46 AM by Christian Cerrito -
Changed lines 5-6 from:

Reads repeatedly from an analog input, calculating a running average and printing it to the computer. Demonstrates the use of arrays.

to:

Reads repeatedly from an analog input, calculating a running average and printing it to the computer. This example is exceptionally useful for smoothing out the values from jumpy/erratic sensors, and also demonstrates the use of arrays to store data.

'Hardware

  • Arduino Board
  • Potentiometer
Deleted lines 12-13:

Potentiometer on analog input pin 0.

Changed lines 18-20 from:
to:

Connect one pin of a potentiometer to 5V, the center pin to analog pin 0, and the the last pin to ground.

August 24, 2010, at 08:20 AM by Christian Cerrito -
Changed line 26 from:
to:
February 23, 2010, at 07:59 PM by Tom Igoe -
Changed line 26 from:
 /*
to:
Deleted lines 27-89:
   Smoothing

   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.
   
   The circuit:
     * Analog sensor (potentiometer will do) attached to analog input 0

   Created 22 April 2007
   By David A. Mellis  <dam@mellis.org>

   http://www.arduino.cc/en/Tutorial/Smoothing

 */

 // 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
 // use this value to determine the size of the readings array.
 const int numReadings = 10;

 int readings[numReadings];      // the readings from the analog input
 int index = 0;                  // the index of the current reading
 int total = 0;                  // the running total
 int average = 0;                // the average

 int inputPin = 0;

 void setup()
 {
   // initialize serial communication with computer:
   Serial.begin(9600);                   
   // initialize all the readings to 0: 
   for (int thisReading = 0; thisReading < numReadings; thisReading++)
     readings[thisReading] = 0;          
 }

 void loop() {
   // subtract the last reading:
   total= total - readings[index];         
   // read from the sensor:  
   readings[index] = analogRead(inputPin); 
   // add the reading to the total:
   total= total + readings[index];       
   // advance to the next position in the array:  
   index = index + 1;                    

   // if we're at the end of the array...
   if (index >= numReadings)              
     // ...wrap around to the beginning: 
     index = 0;                           

   // calculate the average:
   average = total / numReadings;         
   // send it to the computer (as ASCII digits) 
   Serial.println(average, DEC);               
 }

August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 14-16:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:22 PM by Tom Igoe -
Changed lines 21-29 from:

[@ /*

  Smoothing

  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.
to:
Changed lines 23-77 from:
  The circuit:
    * Analog sensor (potentiometer will do) attached to analog input 0

  Created 22 April 2007
  By David A. Mellis  <dam@mellis.org>

  http://www.arduino.cc/en/Tutorial/Smoothing

  • /

// 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 // use this value to determine the size of the readings array. const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average

int inputPin = 0;

void setup() {

  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          

}

void loop() {

  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer (as ASCII digits) 
  Serial.println(average, DEC);               

} @]

to:
June 25, 2009, at 11:29 PM by Tom Igoe -
Added lines 11-19:

click the image to enlarge

Schematic

click the image to enlarge

June 17, 2009, at 11:30 PM by Tom Igoe -
Added lines 14-33:

/*

  Smoothing

  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.

  The circuit:
    * Analog sensor (potentiometer will do) attached to analog input 0

  Created 22 April 2007
  By David A. Mellis  <dam@mellis.org>

  http://www.arduino.cc/en/Tutorial/Smoothing

  • /

Changed line 36 from:

// respond to the input. Using a #define rather than a normal variable lets

to:

// respond to the input. Using a constant rather than a normal variable lets

Changed lines 38-44 from:
  1. define NUMREADINGS 10

int readings[NUMREADINGS]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average

to:

const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average

Changed lines 49-51 from:
  Serial.begin(9600);                     // initialize serial communication with computer
  for (int i = 0; i < NUMREADINGS; i++)
    readings[i] = 0;                      // initialize all the readings to 0
to:
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
Changed lines 56-67 from:

void loop() {

  total -= readings[index];               // subtract the last reading
  readings[index] = analogRead(inputPin); // read from the sensor
  total += readings[index];               // add the reading to the total
  index = (index + 1);                    // advance to the next index

  if (index >= NUMREADINGS)               // if we're at the end of the array...
    index = 0;                            // ...wrap around to the beginning

  average = total / NUMREADINGS;          // calculate the average
  Serial.println(average);                // send it to the computer (as ASCII digits)
to:

void loop() {

  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer (as ASCII digits) 
  Serial.println(average, DEC);               
April 22, 2007, at 07:19 PM by David A. Mellis -
Changed lines 5-6 from:

Reads repeatedly from an analog input, calculating a running average and outputting it to an analog output. Demonstrates the use of arrays.

to:

Reads repeatedly from an analog input, calculating a running average and printing it to the computer. Demonstrates the use of arrays.

Changed lines 9-10 from:

Potentiometer on analog input pin 0, LED on pin 9.

to:

Potentiometer on analog input pin 0.

Deleted lines 13-22:

/*

 * Smoothing
 * David A. Mellis <dam@mellis.org>
 *
 * Reads repeatedly from an analog input, calculating a running average
 * and outputting it to an analog output. 
 *
 * http://www.arduino.cc/en/Tutorial/Smoothing
 */
Changed lines 26-27 from:

int outputPin = 9;

to:
Added line 29:
  Serial.begin(9600);                     // initialize serial communication with computer
Changed line 45 from:
  analogWrite(outputPin, average / 4);    // analog inputs go up to 1023, outputs to 255
to:
  Serial.println(average);                // send it to the computer (as ASCII digits)
March 25, 2007, at 10:02 AM by David A. Mellis -
Added lines 1-12:

Examples > Analog I/O

Smoothing

Reads repeatedly from an analog input, calculating a running average and outputting it to an analog output. Demonstrates the use of arrays.

Circuit

Potentiometer on analog input pin 0, LED on pin 9.

Code

Changed lines 14-22 from:
  1. define NUMSAMPLES 10

int samples[NUMSAMPLES]; int index = 0; int total = 0;

int sensor = 0; int actuator = 9;

to:

/*

 * Smoothing
 * David A. Mellis <dam@mellis.org>
 *
 * Reads repeatedly from an analog input, calculating a running average
 * and outputting it to an analog output. 
 *
 * http://www.arduino.cc/en/Tutorial/Smoothing
 */

// 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 #define rather than a normal variable lets // use this value to determine the size of the readings array.

  1. define NUMREADINGS 10

int readings[NUMREADINGS]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average

int inputPin = 0; int outputPin = 9;

Changed lines 40-41 from:
  for (int i = 0; i < NUMSAMPLES; i++)
    samples[i] = 0;
to:
  for (int i = 0; i < NUMREADINGS; i++)
    readings[i] = 0;                      // initialize all the readings to 0
Changed lines 46-50 from:
  total -= samples[index];
  samples[index] = analogRead(sensor);
  total += samples[index];
  index = (index + 1) % NUMSAMPLES;
  analogWrite(actuator, total / NUMSAMPLES);
to:
  total -= readings[index];               // subtract the last reading
  readings[index] = analogRead(inputPin); // read from the sensor
  total += readings[index];               // add the reading to the total
  index = (index + 1);                    // advance to the next index

  if (index >= NUMREADINGS)               // if we're at the end of the array...
    index = 0;                            // ...wrap around to the beginning

  average = total / NUMREADINGS;          // calculate the average
  analogWrite(outputPin, average / 4);    // analog inputs go up to 1023, outputs to 255
January 14, 2007, at 03:28 PM by David A. Mellis -
Added line 1:

[@

Added line 25:

@]

January 14, 2007, at 03:28 PM by David A. Mellis -
Added lines 1-23:
  1. define NUMSAMPLES 10

int samples[NUMSAMPLES]; int index = 0; int total = 0;

int sensor = 0; int actuator = 9;

void setup() {

  for (int i = 0; i < NUMSAMPLES; i++)
    samples[i] = 0;

}

void loop() {

  total -= samples[index];
  samples[index] = analogRead(sensor);
  total += samples[index];
  index = (index + 1) % NUMSAMPLES;
  analogWrite(actuator, total / NUMSAMPLES);

}

Share