Prototyping an Interactive Experience

Just another WordPress.com weblog

Filtering your Sensor

leave a comment »

Smoothing

Sensors tend to be jittery by nature which intern can cause your application to behave erratically. To smoothen out values we can make use various algorithms.  Many of these algorithms work by saving a fixed number of samples, adding them together, and finally getting the average value.   Here is a program that makes use of such an algorithm.  This program can also be found among your Arduino examples:

// 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.
#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;

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

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)
} 

 
Peak Finding

If you’re building an interaction where its important to grab a specific value you can use an algorithm that just looks for the peak values that sensor puts out. Tom Igo provides a very nice program that does this for you.


/*
Peak Finder

 This example finds the peak value of an analog sensor over time.
 It assumes the sensor is moving in a simple curve.

 The program checks to see that the current value is above a given threshold,
 then checks to see if the value is greater than the previous value. if so,
 then it saves the current value as a peak. When the current value goes below
 the threshold again, it outputs the last peak value recorded.

 created 12 Sept. 2005
 modified 9 Nov. 2006
 by Tom Igoe

 */

int peakValue = 0;
int sensorValue = 0;
int lastSensorValue = 0;
int threshold = 50;             //set your own value based on your sensors
int noise = 5;                  //set a noise value based on your particular sensor

void setup() {
  Serial.begin(9600);
}

void main() {

  //read sensor on pin RA0:
  sensorValue = analogRead(0);

  //check to see that it's above the threshold:
  if ( sensorValue >= threshold + noise ) {
    //if it's greater than the last reading,
    // then make it our current peak:
    if ( sensorValue >= lastsensorValue + noise ) {
      peakValue = sensorValue;
    }
    //if the sensorValue is not above the threshold,
    // then  the last peak value we got would be the actual peak:
  }
  else {
    if ( peakValue >= threshold ) {
      //this is the final peak value; take action
      Serial.print("peak reading: ");
      Serial.print(peakValue, DEC);
    }

    //reset peakValue, since we've finished with this peak:
    peakValue = 0;
  }

  //store the current sensor value for the next loop:
  lastsensorValue = sensorValue;
}

The original program can be found here:
http://www.tigoe.net/pcomp/code/category/-2-2-2-2-2/46 

Written by tankstar

September 23, 2008 at 8:42 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.