implement four ranges

This commit is contained in:
interfisch 2023-04-20 23:16:04 +02:00
parent 5f3e8cc10c
commit 8949c62742
1 changed files with 61 additions and 29 deletions

View File

@ -3,8 +3,11 @@
#include <Arduino.h>
float ec_mean=0;
#define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_RANGE 25
#define EC_PIN_RELAY_RANGEMSB 25
#define EC_PIN_RELAY_RANGELSB 26
#define EC_PIN_ADC 4
@ -14,15 +17,19 @@
#define EC_FREQUENCY 5000
#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;
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;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*4
#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
const uint16_t ec_centerADCvalue=1909; //adc value when probe resistance is equal to the range resistor (mean of both)
unsigned long ec_last_change_relay=0; //millis of last relay change
enum ECState{IDLE,MEASURE};
@ -32,7 +39,7 @@ ECState ecstate=IDLE;
bool ec_measurementReady();
void ec_startMeasurement();
void ec_setRange(bool);
void ec_setRange(uint8_t range);
void ec_connectProbe(bool);
void ec_releaseRelay();
@ -44,7 +51,8 @@ void ec_setup() {
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
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
pinMode(EC_PIN_RELAY_RANGELSB,OUTPUT); //LOW=NC, HIGH=NO
pinMode(EC_PIN_RELAY_RANGEMSB,OUTPUT); //LOW=NC, HIGH=NO
ec_releaseRelay();
}
@ -61,17 +69,28 @@ void ec_loop(unsigned long 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);
float adc_range00=getMean(ec_array_range00,EC_ARRAY_SIZE); //good for low conductivity/high resistance
float adc_range01=getMean(ec_array_range01,EC_ARRAY_SIZE);
float adc_range10=getMean(ec_array_range10,EC_ARRAY_SIZE);
float adc_range11=getMean(ec_array_range11,EC_ARRAY_SIZE); //good for high conductivity/low resistance
Serial.print("EC ADC: Low="); Serial.print(adc_rangelow); Serial.print(" High="); Serial.println(adc_rangehigh);
ec_mean=0; //TODO select right range of all readings
Serial.println("EC ADC");
Serial.println(adc_range00);
Serial.println(adc_range01);
Serial.println(adc_range10);
Serial.println(adc_range11);
ecstate=IDLE;
}
@ -82,26 +101,33 @@ void ec_loop(unsigned long loopmillis) {
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/2<EC_ARRAY_SIZE) { //take reading into array if measurement running
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/4<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);
}
ec_setRange(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);
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;
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;
}
@ -133,18 +159,23 @@ void ec_startMeasurement() {
}
bool ec_measurementReady(){
if (ec_array_pos>=EC_ARRAY_SIZE*2) { //reached end of both arrays
if (ec_array_pos/EC_ARRAY_SIZE>=4) { //reached end of both arrays
return true;
}else{
return false;
}
}
void ec_setRange(bool range) {
void ec_setRange(uint8_t 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);
uint8_t crange=digitalRead(EC_PIN_RELAY_RANGELSB)+2*digitalRead(EC_PIN_RELAY_RANGEMSB);
if (crange!=range) { //write only if different
Serial.print("setRange("); Serial.print(range); Serial.print("), was "); Serial.print(crange);
digitalWrite(EC_PIN_RELAY_RANGELSB,range%2);
digitalWrite(EC_PIN_RELAY_RANGEMSB,range/2);
Serial.print(". Relay set to "); Serial.print(digitalRead(EC_PIN_RELAY_RANGEMSB)); Serial.print(""); Serial.print(digitalRead(EC_PIN_RELAY_RANGELSB)); Serial.println();
ec_last_change_relay=millis();
}
}
@ -159,7 +190,8 @@ void ec_connectProbe(bool relay) {
void ec_releaseRelay() {
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
digitalWrite(EC_PIN_RELAY_RANGE,LOW);
digitalWrite(EC_PIN_RELAY_RANGEMSB,LOW);
digitalWrite(EC_PIN_RELAY_RANGELSB,LOW);
ec_last_change_relay=millis();
}