2023-04-17 19:32:34 +00:00
|
|
|
#ifndef _EC_H_
|
|
|
|
#define _EC_H_
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
2023-04-17 19:33:05 +00:00
|
|
|
#define EC_PIN_RELAY_PROBE 27
|
|
|
|
#define EC_PIN_RELAY_RANGE 25
|
|
|
|
|
2023-04-17 19:32:34 +00:00
|
|
|
|
|
|
|
#define EC_PIN_ADC 4
|
|
|
|
#define EC_PIN_FREQ 5
|
|
|
|
#define EC_PWM_CH 0
|
|
|
|
#define EC_RESOLUTION 8
|
|
|
|
#define EC_FREQUENCY 5000
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
#define EC_ARRAY_SIZE 128
|
|
|
|
uint16_t ec_array_rangeLow[EC_ARRAY_SIZE];
|
|
|
|
uint16_t ec_array_rangeHigh[EC_ARRAY_SIZE];
|
|
|
|
uint16_t ec_array_pos=EC_ARRAY_SIZE*2;
|
|
|
|
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
|
|
|
|
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2
|
|
|
|
#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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
bool ec_measurementReady();
|
|
|
|
void ec_startMeasurement();
|
|
|
|
void ec_setRange(bool);
|
|
|
|
void ec_connectProbe(bool);
|
|
|
|
void ec_releaseRelay();
|
2023-04-17 19:32:34 +00:00
|
|
|
|
|
|
|
void ec_setup() {
|
|
|
|
pinMode(EC_PIN_ADC,INPUT);
|
|
|
|
|
|
|
|
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
|
|
|
|
pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor
|
2023-04-20 19:52:22 +00:00
|
|
|
ec_releaseRelay();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_loop(unsigned long loopmillis) {
|
|
|
|
static unsigned long last_measurement_ec=0;
|
|
|
|
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;
|
|
|
|
Serial.println("EC Take Measurement");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEASURE:
|
|
|
|
if (ec_measurementReady()) {
|
|
|
|
ec_releaseRelay();
|
|
|
|
float adc_rangelow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
|
|
|
|
float adc_rangehigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);
|
|
|
|
|
2023-04-20 19:53:41 +00:00
|
|
|
Serial.print("EC ADC: Low="); Serial.print(adc_rangelow); Serial.print(" High="); Serial.println(adc_rangehigh);
|
|
|
|
|
2023-04-20 19:52:22 +00:00
|
|
|
ecstate=IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/2<EC_ARRAY_SIZE) { //take reading into array if measurement running
|
|
|
|
last_read_ec=loopmillis;
|
|
|
|
|
|
|
|
//flag_print= ec_array_pos==EC_ARRAY_SIZE;
|
|
|
|
//ec_array_pos%=EC_ARRAY_SIZE;
|
|
|
|
|
|
|
|
if (ec_array_pos<EC_ARRAY_SIZE){ //low range
|
|
|
|
ec_setRange(LOW);
|
|
|
|
}else{ //high range
|
|
|
|
ec_setRange(HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
|
|
|
|
uint16_t value=analogRead(EC_PIN_ADC);
|
|
|
|
if (ec_array_pos<EC_ARRAY_SIZE){ //low range
|
|
|
|
ec_array_rangeLow[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
|
|
|
|
}else{ //high range
|
|
|
|
ec_array_rangeHigh[ec_array_pos%EC_ARRAY_SIZE]=value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (ec_array_pos==0) {
|
|
|
|
Serial.println(""); Serial.print("Lowrange:");
|
|
|
|
}
|
|
|
|
if (ec_array_pos==EC_ARRAY_SIZE) {
|
|
|
|
Serial.println(""); Serial.print("Highrange:");
|
|
|
|
}
|
|
|
|
Serial.print(value); Serial.print(" ");
|
|
|
|
if (ec_array_pos==EC_ARRAY_SIZE*2-1) {
|
|
|
|
Serial.println("");
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
ec_array_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ec_startMeasurement() {
|
|
|
|
ec_array_pos=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ec_measurementReady(){
|
|
|
|
if (ec_array_pos>=EC_ARRAY_SIZE*2) { //reached end of both arrays
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ec_setRange(bool range) {
|
|
|
|
//range low means low resistor value -> NO -> relay High
|
|
|
|
bool val=digitalRead(EC_PIN_RELAY_RANGE);
|
|
|
|
if (val!=!range) { //write only if different
|
|
|
|
digitalWrite(EC_PIN_RELAY_RANGE,!range);
|
|
|
|
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);
|
|
|
|
digitalWrite(EC_PIN_RELAY_RANGE,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
|