2023-04-17 19:32:34 +00:00
|
|
|
#ifndef _EC_H_
|
|
|
|
#define _EC_H_
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
float ecEC_ADS_CHANNEL_mean=0;
|
|
|
|
|
|
|
|
bool ec_flag_measurement_available=false;
|
2023-04-20 21:16:04 +00:00
|
|
|
|
2023-04-17 19:33:05 +00:00
|
|
|
#define EC_PIN_RELAY_PROBE 27
|
2023-04-20 21:16:04 +00:00
|
|
|
#define EC_PIN_RELAY_RANGEMSB 25
|
|
|
|
#define EC_PIN_RELAY_RANGELSB 26
|
2023-04-17 19:33:05 +00:00
|
|
|
|
2023-04-17 19:32:34 +00:00
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
//#define EC_PIN_ADC 4
|
|
|
|
#define EC_ADS_CHANNEL 0
|
2023-04-17 19:32:34 +00:00
|
|
|
#define EC_PIN_FREQ 5
|
|
|
|
#define EC_PWM_CH 0
|
|
|
|
#define EC_RESOLUTION 8
|
|
|
|
#define EC_FREQUENCY 5000
|
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
#define EC_ARRAY_SIZE 64
|
2023-04-20 21:16:04 +00:00
|
|
|
uint16_t ec_array_range00[EC_ARRAY_SIZE]; //00=NC,NC = highest value
|
|
|
|
uint16_t ec_array_range01[EC_ARRAY_SIZE];
|
|
|
|
uint16_t ec_array_range10[EC_ARRAY_SIZE];
|
|
|
|
uint16_t ec_array_range11[EC_ARRAY_SIZE]; //11= NO,NO = lowest value
|
|
|
|
uint16_t ec_array_pos=EC_ARRAY_SIZE*4;
|
2023-04-22 01:14:51 +00:00
|
|
|
unsigned long last_measurement_ec=0;
|
2023-04-20 19:52:22 +00:00
|
|
|
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
|
2023-04-20 21:16:04 +00:00
|
|
|
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
|
2023-04-22 01:14:51 +00:00
|
|
|
#define EC_READ_INTERVAL 2 //interval of reading adc value inside a measurement
|
2023-04-20 19:52:22 +00:00
|
|
|
|
|
|
|
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
|
|
|
|
|
2023-04-20 21:16:04 +00:00
|
|
|
const uint16_t ec_centerADCvalue=1909; //adc value when probe resistance is equal to the range resistor (mean of both)
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
unsigned long ec_last_change_relay=0; //millis of last relay change
|
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
enum ECState{IDLE,MEASURE};
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
ECState ecstate=IDLE;
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
float adc_range00;
|
|
|
|
float adc_range01;
|
|
|
|
float adc_range10;
|
|
|
|
float adc_range11;
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
|
|
|
|
bool ec_measurementReady();
|
|
|
|
void ec_startMeasurement();
|
2023-04-20 21:16:04 +00:00
|
|
|
void ec_setRange(uint8_t range);
|
2023-04-20 19:52:22 +00:00
|
|
|
void ec_connectProbe(bool);
|
|
|
|
void ec_releaseRelay();
|
2023-04-17 19:32:34 +00:00
|
|
|
|
|
|
|
void ec_setup() {
|
2023-04-22 01:14:51 +00:00
|
|
|
//pinMode(EC_PIN_ADC,INPUT);
|
2023-04-17 19:32:34 +00:00
|
|
|
|
|
|
|
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
|
|
|
|
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
|
2023-04-20 19:52:22 +00:00
|
|
|
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
|
2023-04-17 19:32:34 +00:00
|
|
|
|
2023-04-17 19:33:05 +00:00
|
|
|
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
|
2023-04-20 21:16:04 +00:00
|
|
|
pinMode(EC_PIN_RELAY_RANGELSB,OUTPUT); //LOW=NC, HIGH=NO
|
|
|
|
pinMode(EC_PIN_RELAY_RANGEMSB,OUTPUT); //LOW=NC, HIGH=NO
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_releaseRelay();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_loop(unsigned long loopmillis) {
|
2023-04-22 01:14:51 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
static unsigned long last_read_ec=0;
|
|
|
|
|
|
|
|
switch (ecstate) {
|
|
|
|
case IDLE:
|
|
|
|
|
|
|
|
if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle
|
|
|
|
last_measurement_ec=loopmillis;
|
|
|
|
ec_startMeasurement();
|
|
|
|
ec_connectProbe(true);
|
|
|
|
ecstate=MEASURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEASURE:
|
|
|
|
if (ec_measurementReady()) {
|
|
|
|
ec_releaseRelay();
|
2023-04-22 01:14:51 +00:00
|
|
|
adc_range00=getMean(ec_array_range00,EC_ARRAY_SIZE); //good for low conductivity/high resistance
|
|
|
|
adc_range01=getMean(ec_array_range01,EC_ARRAY_SIZE);
|
|
|
|
adc_range10=getMean(ec_array_range10,EC_ARRAY_SIZE);
|
|
|
|
adc_range11=getMean(ec_array_range11,EC_ARRAY_SIZE); //good for high conductivity/low resistance
|
2023-04-20 21:16:04 +00:00
|
|
|
|
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
//ec_mean=0; //TODO select right range of all readings
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-22 01:14:51 +00:00
|
|
|
ec_flag_measurement_available=true;
|
2023-04-20 21:16:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
ecstate=IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-20 21:16:04 +00:00
|
|
|
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/4<EC_ARRAY_SIZE) { //take reading into array if measurement running
|
2023-04-20 19:52:22 +00:00
|
|
|
last_read_ec=loopmillis;
|
|
|
|
|
|
|
|
//flag_print= ec_array_pos==EC_ARRAY_SIZE;
|
|
|
|
//ec_array_pos%=EC_ARRAY_SIZE;
|
|
|
|
|
2023-04-20 21:16:04 +00:00
|
|
|
ec_setRange(ec_array_pos/EC_ARRAY_SIZE);
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
|
|
|
|
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
|
2023-04-22 01:14:51 +00:00
|
|
|
//uint16_t value=analogRead(EC_PIN_ADC);
|
|
|
|
|
|
|
|
uint16_t value = ADS.readADC(EC_ADS_CHANNEL);
|
2023-04-20 21:16:04 +00:00
|
|
|
switch (ec_array_pos/EC_ARRAY_SIZE){ //low range
|
|
|
|
case 0:
|
|
|
|
ec_array_range00[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
ec_array_range01[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
ec_array_range10[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
ec_array_range11[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
break;
|
2023-04-20 19:52:22 +00:00
|
|
|
}
|
|
|
|
ec_array_pos++;
|
2023-04-22 01:14:51 +00:00
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ec_startMeasurement() {
|
|
|
|
ec_array_pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ec_measurementReady(){
|
2023-04-20 21:16:04 +00:00
|
|
|
if (ec_array_pos/EC_ARRAY_SIZE>=4) { //reached end of both arrays
|
2023-04-20 19:52:22 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 21:16:04 +00:00
|
|
|
void ec_setRange(uint8_t range) {
|
2023-04-20 19:52:22 +00:00
|
|
|
//range low means low resistor value -> NO -> relay High
|
2023-04-20 21:16:04 +00:00
|
|
|
|
|
|
|
uint8_t crange=digitalRead(EC_PIN_RELAY_RANGELSB)+2*digitalRead(EC_PIN_RELAY_RANGEMSB);
|
|
|
|
|
|
|
|
if (crange!=range) { //write only if different
|
|
|
|
digitalWrite(EC_PIN_RELAY_RANGELSB,range%2);
|
|
|
|
digitalWrite(EC_PIN_RELAY_RANGEMSB,range/2);
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_last_change_relay=millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_connectProbe(bool relay) {
|
|
|
|
bool val=digitalRead(EC_PIN_RELAY_PROBE);
|
|
|
|
if (val!=relay) { //write only if different
|
|
|
|
digitalWrite(EC_PIN_RELAY_PROBE,relay);
|
|
|
|
ec_last_change_relay=millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_releaseRelay() {
|
2023-04-17 19:33:05 +00:00
|
|
|
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
|
2023-04-20 21:16:04 +00:00
|
|
|
digitalWrite(EC_PIN_RELAY_RANGEMSB,LOW);
|
|
|
|
digitalWrite(EC_PIN_RELAY_RANGELSB,LOW);
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_last_change_relay=millis();
|
2023-04-17 19:32:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
|
2023-04-17 19:32:34 +00:00
|
|
|
#endif
|