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
00001 // SmoothAnalogIn.h: Martyn Stopps / Taneli Holtta : 18-06-10 00002 00003 // Header file: included by other code so we what is available (the declaration) 00004 00005 // SmoothAnalogIn class that takes arguments PinName, adc sample rate, smoothing factor and scaling factor 00006 // PinName: P15..20 (analogue inputs) 00007 // adc sampling rate: duration in seconds ie 0.01 = 100Hz (a ticker in the class takes care of sampling) 00008 // smoothing factor: 1 to x (smoothed value +-difference)/x 00009 // 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 00010 00011 // To use the class 00012 // #include "SmoothAnalogIn.h" in main.cpp // include SmoothAnalogIn header file 00013 // SmoothAnalogIn tc1(p15,0.01,5,3300); // (PinName, sampleRate, smoothingFactor, adcScaling) 00014 // SmoothAnalogIn tc2(p16,0.01,500,3300); // (PinName, sampleRate, smoothingFactor, adcScaling) 00015 // tc1.read() // return tc1 smoothed adc value 00016 00017 00018 #include "mbed.h" // tested with revision 23 00019 00020 class SmoothAnalogIn { // Class 00021 00022 public: 00023 00024 SmoothAnalogIn(PinName pin, float sampleRate, int smoothingFactor, int adcScaling); 00025 00026 double read (void); // public- returns smoothed adc value 00027 00028 00029 private: 00030 00031 // private objects 00032 00033 AnalogIn _adc; // AnalogIn 00034 Ticker _ticker; // Ticker within class 00035 00036 // private functions 00037 00038 void smoothValue(double); // function to smooth adc value 00039 void sampleAdc(void); // gets current adc value and then calls smoothing function 'void smoothValue(double);' 00040 00041 00042 // private variables 00043 00044 double smoothed; // smoothed adc value 00045 int _smoothingFactor; // instance value - smoothing factor 00046 int _adcScaling; // instance value -adc scaling factor 00047 00048 };
Generated on Wed Jul 13 2022 18:07:28 by 1.7.2