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.
SmoothAnalogIn.h@0:d252aa8196fc, 2010-06-18 (annotated)
- Committer:
- stoppsm
- Date:
- Fri Jun 18 13:54:29 2010 +0000
- Revision:
- 0:d252aa8196fc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stoppsm | 0:d252aa8196fc | 1 | // SmoothAnalogIn.h: Martyn Stopps / Taneli Holtta : 18-06-10 |
stoppsm | 0:d252aa8196fc | 2 | |
stoppsm | 0:d252aa8196fc | 3 | // Header file: included by other code so we what is available (the declaration) |
stoppsm | 0:d252aa8196fc | 4 | |
stoppsm | 0:d252aa8196fc | 5 | // SmoothAnalogIn class that takes arguments PinName, adc sample rate, smoothing factor and scaling factor |
stoppsm | 0:d252aa8196fc | 6 | // PinName: P15..20 (analogue inputs) |
stoppsm | 0:d252aa8196fc | 7 | // adc sampling rate: duration in seconds ie 0.01 = 100Hz (a ticker in the class takes care of sampling) |
stoppsm | 0:d252aa8196fc | 8 | // smoothing factor: 1 to x (smoothed value +-difference)/x |
stoppsm | 0:d252aa8196fc | 9 | // 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 |
stoppsm | 0:d252aa8196fc | 10 | |
stoppsm | 0:d252aa8196fc | 11 | // To use the class |
stoppsm | 0:d252aa8196fc | 12 | // #include "SmoothAnalogIn.h" in main.cpp // include SmoothAnalogIn header file |
stoppsm | 0:d252aa8196fc | 13 | // SmoothAnalogIn tc1(p15,0.01,5,3300); // (PinName, sampleRate, smoothingFactor, adcScaling) |
stoppsm | 0:d252aa8196fc | 14 | // SmoothAnalogIn tc2(p16,0.01,500,3300); // (PinName, sampleRate, smoothingFactor, adcScaling) |
stoppsm | 0:d252aa8196fc | 15 | // tc1.read() // return tc1 smoothed adc value |
stoppsm | 0:d252aa8196fc | 16 | |
stoppsm | 0:d252aa8196fc | 17 | |
stoppsm | 0:d252aa8196fc | 18 | #include "mbed.h" // tested with revision 23 |
stoppsm | 0:d252aa8196fc | 19 | |
stoppsm | 0:d252aa8196fc | 20 | class SmoothAnalogIn { // Class |
stoppsm | 0:d252aa8196fc | 21 | |
stoppsm | 0:d252aa8196fc | 22 | public: |
stoppsm | 0:d252aa8196fc | 23 | |
stoppsm | 0:d252aa8196fc | 24 | SmoothAnalogIn(PinName pin, float sampleRate, int smoothingFactor, int adcScaling); |
stoppsm | 0:d252aa8196fc | 25 | |
stoppsm | 0:d252aa8196fc | 26 | double read (void); // public- returns smoothed adc value |
stoppsm | 0:d252aa8196fc | 27 | |
stoppsm | 0:d252aa8196fc | 28 | |
stoppsm | 0:d252aa8196fc | 29 | private: |
stoppsm | 0:d252aa8196fc | 30 | |
stoppsm | 0:d252aa8196fc | 31 | // private objects |
stoppsm | 0:d252aa8196fc | 32 | |
stoppsm | 0:d252aa8196fc | 33 | AnalogIn _adc; // AnalogIn |
stoppsm | 0:d252aa8196fc | 34 | Ticker _ticker; // Ticker within class |
stoppsm | 0:d252aa8196fc | 35 | |
stoppsm | 0:d252aa8196fc | 36 | // private functions |
stoppsm | 0:d252aa8196fc | 37 | |
stoppsm | 0:d252aa8196fc | 38 | void smoothValue(double); // function to smooth adc value |
stoppsm | 0:d252aa8196fc | 39 | void sampleAdc(void); // gets current adc value and then calls smoothing function 'void smoothValue(double);' |
stoppsm | 0:d252aa8196fc | 40 | |
stoppsm | 0:d252aa8196fc | 41 | |
stoppsm | 0:d252aa8196fc | 42 | // private variables |
stoppsm | 0:d252aa8196fc | 43 | |
stoppsm | 0:d252aa8196fc | 44 | double smoothed; // smoothed adc value |
stoppsm | 0:d252aa8196fc | 45 | int _smoothingFactor; // instance value - smoothing factor |
stoppsm | 0:d252aa8196fc | 46 | int _adcScaling; // instance value -adc scaling factor |
stoppsm | 0:d252aa8196fc | 47 | |
stoppsm | 0:d252aa8196fc | 48 | }; |