IFD:PhysicalComp2011/code: Difference between revisions

From Medien Wiki
(Added capacitive sensing (Arduino))
Line 1,073: Line 1,073:
   delay(10);                         
   delay(10);                         
}
}
</source>
<br /> <br />
=== Va Smoothing Sensor Values ===
[[File:Arduino_array_smoothing.png|200px|thumb]]
<source lang="java">
/* SMOOTHING SENSOR VALUES (ARDUINO)
*
* Sometimes the values from a sensor variate
* around a certain value or have certain peaks
* that we need to filter out in order to
* work with the signal. For this we can
* use an array in which we write the
* sensor reaings first and calculate the
* average of all values in the array.
*
* NOTE: This smoothing example is very simple.
* There are much better ways of doing this, but
* this is the most basic procedure to illustrate
* the purpose of a smoothing algorithm.
*
* Frederic Gmeiner, 2011
*/
#include <CapSense.h>
CapSense  cs_9_2 = CapSense(9,2);
// vars for smoothing array:
const int numReadings = 10;
long capValues[numReadings];
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
void setup()                   
{
  Serial.begin(9600);
}
void loop()                   
  capValues[index] = cs_9_2.capSense(30);
  // reset and recalculate the total:
  total= 0;
  for(int i=0; i<numReadings; i++) total += capValues[i];
 
  // advance to the next position in the array: 
  index = index + 1;
 
  // if we're at the end of the array...
  // ...wrap around to the beginning:
  if (index >= numReadings) index = 0;
 
  // calculate the average:
  average = total / numReadings; 
 
  Serial.println(average);        // print the average
  delay(10);                      // arbitrary delay to limit data to serial port
}
</source>
</source>
<br /> <br />
<br /> <br />