The SmoothAnalogIn class provides a smoothed ADC reading; simply setup pin, sample rate, smoothing factor and scaling factor. Useful for processing signals from temperature or pressure sensors etc.
Revision 0:d252aa8196fc, committed 2010-06-18
- Comitter:
- stoppsm
- Date:
- Fri Jun 18 13:54:29 2010 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SmoothAnalogIn.cpp Fri Jun 18 13:54:29 2010 +0000
@@ -0,0 +1,45 @@
+// SmoothAnalogIn.cpp: Martyn Stopps / Taneli Holtta : 18-06-10
+
+// Source file: contains implementation that generates the code (the definition)
+
+#include "SmoothAnalogIn.h"
+#include "mbed.h" // tested with revision 23
+
+// Constructor - A class that takes arguments PinName, adc sample rate, smoothing factor and scaling factor
+
+SmoothAnalogIn::SmoothAnalogIn(PinName pin, float sampleRate, int smoothingFactor, int adcScaling) : _adc(pin) { // _adc(pin) means pass pin to the AnalogIn constructor
+
+ _ticker.attach(this, &SmoothAnalogIn::sampleAdc, sampleRate); // attach ticker to member function (delivers adc sampleRate)
+
+// initialize variables
+
+ _smoothingFactor = smoothingFactor; // mask in the smoothing factor value
+ _adcScaling = adcScaling; // mask in the adc scaling factor (adc returns 0 to 1 representing 0 to 3.3V)
+
+ }
+
+
+// function - sampleAdc : gets current adc value.
+
+void SmoothAnalogIn::sampleAdc(void) {
+
+ double adc_value = _adc.read()*_adcScaling; // read _adc pin analog value * _adcScaling
+ SmoothAnalogIn::smoothValue(adc_value); // call smoothing function passing in current scaled adc_value
+ }
+
+
+// function - smoothValue : Smoothing algorithm
+
+void SmoothAnalogIn::smoothValue(double newValue) {
+
+ if (newValue > smoothed) smoothed = smoothed + ((newValue - smoothed)/_smoothingFactor); // rawValue > smoothed value
+ else if (newValue < smoothed) smoothed = smoothed - ((smoothed - newValue)/_smoothingFactor); // rawValue < smoothed value
+ }
+
+
+ // function - read : returns smoothed adc value
+
+double SmoothAnalogIn::read(void) {
+
+ return(smoothed);
+ }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SmoothAnalogIn.h Fri Jun 18 13:54:29 2010 +0000
@@ -0,0 +1,48 @@
+// SmoothAnalogIn.h: Martyn Stopps / Taneli Holtta : 18-06-10
+
+// Header file: included by other code so we what is available (the declaration)
+
+// SmoothAnalogIn class that takes arguments PinName, adc sample rate, smoothing factor and scaling factor
+// PinName: P15..20 (analogue inputs)
+// adc sampling rate: duration in seconds ie 0.01 = 100Hz (a ticker in the class takes care of sampling)
+// smoothing factor: 1 to x (smoothed value +-difference)/x
+// scaling factor: adc returns float O to 1 representing 0 to 3.3V) ie a scaling factor of 3300 returns actual adc input in mV
+
+// To use the class
+// #include "SmoothAnalogIn.h" in main.cpp // include SmoothAnalogIn header file
+// SmoothAnalogIn tc1(p15,0.01,5,3300); // (PinName, sampleRate, smoothingFactor, adcScaling)
+// SmoothAnalogIn tc2(p16,0.01,500,3300); // (PinName, sampleRate, smoothingFactor, adcScaling)
+// tc1.read() // return tc1 smoothed adc value
+
+
+#include "mbed.h" // tested with revision 23
+
+class SmoothAnalogIn { // Class
+
+ public:
+
+ SmoothAnalogIn(PinName pin, float sampleRate, int smoothingFactor, int adcScaling);
+
+ double read (void); // public- returns smoothed adc value
+
+
+ private:
+
+ // private objects
+
+ AnalogIn _adc; // AnalogIn
+ Ticker _ticker; // Ticker within class
+
+ // private functions
+
+ void smoothValue(double); // function to smooth adc value
+ void sampleAdc(void); // gets current adc value and then calls smoothing function 'void smoothValue(double);'
+
+
+ // private variables
+
+ double smoothed; // smoothed adc value
+ int _smoothingFactor; // instance value - smoothing factor
+ int _adcScaling; // instance value -adc scaling factor
+
+ };
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Jun 18 13:54:29 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/3944f1e2fa4f