add reference adc measurement

This commit is contained in:
interfisch 2023-04-30 19:00:12 +02:00
parent 390aaeaeb7
commit 833a7fb578
3 changed files with 51 additions and 29 deletions

View File

@ -3,7 +3,6 @@
#include <Arduino.h>
float ecEC_ADS_CHANNEL_mean=0;
bool ec_flag_measurement_available=false;
@ -17,13 +16,18 @@ bool ec_flag_measurement_available=false;
#define EC_RESOLUTION 8
#define EC_FREQUENCY 5000
#define EC_CALIB_ARRAY_SIZE 64
uint16_t ec_calib_array[EC_CALIB_ARRAY_SIZE];
uint16_t ec_calib_array_pos=0;
#define EC_CALIB_READ_INTERVAL 100 //interval of reading adc value inside a measurement
#define EC_ARRAY_SIZE 64
uint16_t ec_array[EC_ARRAY_SIZE];
uint16_t ec_array_pos=EC_ARRAY_SIZE;
unsigned long last_measurement_ec=0;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
#define EC_READ_INTERVAL 2 //interval of reading adc value inside a measurement
#define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
@ -36,6 +40,10 @@ enum ECState{IDLE,MEASURE};
ECState ecstate=IDLE;
float ec_adc;
float ec_adc_adjusted; //adjusted for reference resistor
float ec_calib_adc;
float ec_reference_adc=5889; //adc reference value for the calibration resistor measurement
bool ec_measurementReady();
void ec_startMeasurement();
@ -44,8 +52,6 @@ void ec_connectProbe(bool);
void ec_releaseRelay();
void ec_setup() {
//pinMode(EC_PIN_ADC,INPUT);
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
@ -76,13 +82,13 @@ void ec_loop(unsigned long loopmillis) {
ec_releaseRelay();
Serial.println("Finished EC");
ec_adc=getMean(ec_array,EC_ARRAY_SIZE);
if (isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,0)){
ec_calib_adc=getMean(ec_calib_array,EC_CALIB_ARRAY_SIZE);
ec_adc_adjusted=mapf(ec_adc,0,ec_calib_adc,0,ec_reference_adc);
}
ec_flag_measurement_available=true;
ecstate=IDLE;
}
@ -91,26 +97,33 @@ void ec_loop(unsigned long loopmillis) {
}
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos<EC_ARRAY_SIZE) { //take reading into array if measurement running
if (ec_array_pos<EC_ARRAY_SIZE) { //measurement running
if (loopmillis>last_read_ec+EC_READ_INTERVAL) { //take reading into array
last_read_ec=loopmillis;
//flag_print= ec_array_pos==EC_ARRAY_SIZE;
//ec_array_pos%=EC_ARRAY_SIZE;
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
//uint16_t value=analogRead(EC_PIN_ADC);
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
ec_array[ec_array_pos]=value;
ec_array_pos++;
}
}
}else{ //measurement not running, then take calibration readings
if (loopmillis>last_read_ec+EC_CALIB_READ_INTERVAL) { //take reading into array
last_read_ec=loopmillis;
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
ec_calib_array[ec_calib_array_pos]=value;
ec_calib_array_pos++;
ec_calib_array_pos%=EC_CALIB_ARRAY_SIZE;
}
}
}
}

View File

@ -13,6 +13,7 @@ float getMinf(float *parray, uint16_t psize);
bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck);
bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck);
float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff);
float mapf(float x, float in_min, float in_max, float out_min, float out_max);
float getMean(uint16_t *parray,uint16_t psize) {
@ -113,5 +114,10 @@ float getFilteredf(float *parray,uint16_t psize, uint16_t pcutOff) {
return mean/(psize-2*pcutOff);
}
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#endif

View File

@ -61,7 +61,7 @@ void setup() {
//Serial.println("Setup finished");
delay(200);
Serial.println("time,tempReservoir,EC");
Serial.println("time,tempReservoir,EC,ECadjusted,ECcalib");
}
@ -108,7 +108,10 @@ void loop() {
Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(",");
//Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(",");
Serial.print(ec_adc);
Serial.print(ec_adc); Serial.print(",");
Serial.print(ec_adc_adjusted); Serial.print(",");
Serial.print(ec_calib_adc);
Serial.println();