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.

Dependencies:   mbed

Committer:
stoppsm
Date:
Fri Jun 18 13:54:29 2010 +0000
Revision:
0:d252aa8196fc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stoppsm 0:d252aa8196fc 1 // SmoothAnalogIn.cpp: Martyn Stopps / Taneli Holtta : 18-06-10
stoppsm 0:d252aa8196fc 2
stoppsm 0:d252aa8196fc 3 // Source file: contains implementation that generates the code (the definition)
stoppsm 0:d252aa8196fc 4
stoppsm 0:d252aa8196fc 5 #include "SmoothAnalogIn.h"
stoppsm 0:d252aa8196fc 6 #include "mbed.h" // tested with revision 23
stoppsm 0:d252aa8196fc 7
stoppsm 0:d252aa8196fc 8 // Constructor - A class that takes arguments PinName, adc sample rate, smoothing factor and scaling factor
stoppsm 0:d252aa8196fc 9
stoppsm 0:d252aa8196fc 10 SmoothAnalogIn::SmoothAnalogIn(PinName pin, float sampleRate, int smoothingFactor, int adcScaling) : _adc(pin) { // _adc(pin) means pass pin to the AnalogIn constructor
stoppsm 0:d252aa8196fc 11
stoppsm 0:d252aa8196fc 12 _ticker.attach(this, &SmoothAnalogIn::sampleAdc, sampleRate); // attach ticker to member function (delivers adc sampleRate)
stoppsm 0:d252aa8196fc 13
stoppsm 0:d252aa8196fc 14 // initialize variables
stoppsm 0:d252aa8196fc 15
stoppsm 0:d252aa8196fc 16 _smoothingFactor = smoothingFactor; // mask in the smoothing factor value
stoppsm 0:d252aa8196fc 17 _adcScaling = adcScaling; // mask in the adc scaling factor (adc returns 0 to 1 representing 0 to 3.3V)
stoppsm 0:d252aa8196fc 18
stoppsm 0:d252aa8196fc 19 }
stoppsm 0:d252aa8196fc 20
stoppsm 0:d252aa8196fc 21
stoppsm 0:d252aa8196fc 22 // function - sampleAdc : gets current adc value.
stoppsm 0:d252aa8196fc 23
stoppsm 0:d252aa8196fc 24 void SmoothAnalogIn::sampleAdc(void) {
stoppsm 0:d252aa8196fc 25
stoppsm 0:d252aa8196fc 26 double adc_value = _adc.read()*_adcScaling; // read _adc pin analog value * _adcScaling
stoppsm 0:d252aa8196fc 27 SmoothAnalogIn::smoothValue(adc_value); // call smoothing function passing in current scaled adc_value
stoppsm 0:d252aa8196fc 28 }
stoppsm 0:d252aa8196fc 29
stoppsm 0:d252aa8196fc 30
stoppsm 0:d252aa8196fc 31 // function - smoothValue : Smoothing algorithm
stoppsm 0:d252aa8196fc 32
stoppsm 0:d252aa8196fc 33 void SmoothAnalogIn::smoothValue(double newValue) {
stoppsm 0:d252aa8196fc 34
stoppsm 0:d252aa8196fc 35 if (newValue > smoothed) smoothed = smoothed + ((newValue - smoothed)/_smoothingFactor); // rawValue > smoothed value
stoppsm 0:d252aa8196fc 36 else if (newValue < smoothed) smoothed = smoothed - ((smoothed - newValue)/_smoothingFactor); // rawValue < smoothed value
stoppsm 0:d252aa8196fc 37 }
stoppsm 0:d252aa8196fc 38
stoppsm 0:d252aa8196fc 39
stoppsm 0:d252aa8196fc 40 // function - read : returns smoothed adc value
stoppsm 0:d252aa8196fc 41
stoppsm 0:d252aa8196fc 42 double SmoothAnalogIn::read(void) {
stoppsm 0:d252aa8196fc 43
stoppsm 0:d252aa8196fc 44 return(smoothed);
stoppsm 0:d252aa8196fc 45 }