alpahsense

Dependents:   Example_DS3231_test

Committer:
etiene32
Date:
Wed Mar 06 14:01:39 2019 +0000
Revision:
0:ed1f58bf0dc3
sd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
etiene32 0:ed1f58bf0dc3 1 #include "Alphasense.h"
etiene32 0:ed1f58bf0dc3 2 #include "Adafruit_ADS1015.h"
etiene32 0:ed1f58bf0dc3 3
etiene32 0:ed1f58bf0dc3 4 ALPHASENSE::ALPHASENSE(I2C* i2c, int num_chan_op1, int num_chan_op2, int we0, int ae0, int sensitivity, int n_sample) : _i2c(i2c), _num_chan_op1(num_chan_op1), _num_chan_op2(num_chan_op2), _we0(we0), _ae0(ae0), _sensitivity(sensitivity), _n_sample(n_sample) {
etiene32 0:ed1f58bf0dc3 5 }
etiene32 0:ed1f58bf0dc3 6
etiene32 0:ed1f58bf0dc3 7 float ALPHASENSE::measure() {
etiene32 0:ed1f58bf0dc3 8 double we_result, ae_result;
etiene32 0:ed1f58bf0dc3 9 float result;
etiene32 0:ed1f58bf0dc3 10 int we, ae, we_integral, ae_integral;
etiene32 0:ed1f58bf0dc3 11
etiene32 0:ed1f58bf0dc3 12 Adafruit_ADS1115 ads(_i2c); //init ads1115
etiene32 0:ed1f58bf0dc3 13 ads.setGain(GAIN_TWO); //set range to 2.048 V
etiene32 0:ed1f58bf0dc3 14
etiene32 0:ed1f58bf0dc3 15 for (int i=0; i<_n_sample; i++)
etiene32 0:ed1f58bf0dc3 16 {
etiene32 0:ed1f58bf0dc3 17 we = ads.readADC_SingleEnded(_num_chan_op1); // read channel
etiene32 0:ed1f58bf0dc3 18 ae = ads.readADC_SingleEnded(_num_chan_op2);
etiene32 0:ed1f58bf0dc3 19 we_integral += we;
etiene32 0:ed1f58bf0dc3 20 ae_integral += ae;
etiene32 0:ed1f58bf0dc3 21 }
etiene32 0:ed1f58bf0dc3 22
etiene32 0:ed1f58bf0dc3 23 we_result = we_integral / _n_sample; //smoothing result
etiene32 0:ed1f58bf0dc3 24 ae_result = ae_integral / _n_sample;
etiene32 0:ed1f58bf0dc3 25
etiene32 0:ed1f58bf0dc3 26 we_result = we_result * 0.0625; //conversion to mV (2048 / 65536)
etiene32 0:ed1f58bf0dc3 27 ae_result = ae_result * 0.0625;
etiene32 0:ed1f58bf0dc3 28
etiene32 0:ed1f58bf0dc3 29 we_integral = 0; //reset integral
etiene32 0:ed1f58bf0dc3 30 ae_integral = 0;
etiene32 0:ed1f58bf0dc3 31
etiene32 0:ed1f58bf0dc3 32 result = (((we_result - _we0) - (ae_result - _ae0)) / _sensitivity) * 1000; //conversion to ppb
etiene32 0:ed1f58bf0dc3 33
etiene32 0:ed1f58bf0dc3 34 return result;
etiene32 0:ed1f58bf0dc3 35 }