Compare commits

..

3 Commits

5 changed files with 151 additions and 33 deletions

View File

@ -4,12 +4,8 @@
#include <Arduino.h> #include <Arduino.h>
#define EC_PIN_RELAY_PROBE 27 #define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_CALIBRATION 26
#define EC_PIN_RELAY_RANGE 25 #define EC_PIN_RELAY_RANGE 25
#define EC_CALIBRATION_RESISTOR_NC 100000
#define EC_CALIBRATION_RESISTOR_NO 1000
#define EC_PIN_ADC 4 #define EC_PIN_ADC 4
#define EC_PIN_FREQ 5 #define EC_PIN_FREQ 5
@ -17,37 +13,155 @@
#define EC_RESOLUTION 8 #define EC_RESOLUTION 8
#define EC_FREQUENCY 5000 #define EC_FREQUENCY 5000
#define EC_ARRAY_SIZE 1024 #define EC_ARRAY_SIZE 128
uint16_t ec_array[EC_ARRAY_SIZE]; uint16_t ec_array_rangeLow[EC_ARRAY_SIZE];
uint16_t ec_array_pos=0; uint16_t ec_array_rangeHigh[EC_ARRAY_SIZE];
unsigned long last_read_ec=0; uint16_t ec_array_pos=EC_ARRAY_SIZE*2;
#define EC_READ_INTERVAL 1 #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
enum ECState{IDLE,MEASURE};
ECState ecstate=IDLE;
bool ec_measurementReady();
void ec_startMeasurement();
void ec_setRange(bool);
void ec_connectProbe(bool);
void ec_releaseRelay();
void ec_setup() { void ec_setup() {
pinMode(EC_PIN_ADC,INPUT); pinMode(EC_PIN_ADC,INPUT);
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION); ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127); ledcWrite(EC_PWM_CH, 127); //50% duty cycle
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
pinMode(EC_PIN_RELAY_CALIBRATION,OUTPUT); //LOW=NC Calibration Resistor, HIGH=NO Calib. Res.
pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor
digitalWrite(EC_PIN_RELAY_PROBE,LOW); ec_releaseRelay();
digitalWrite(EC_PIN_RELAY_CALIBRATION,LOW);
digitalWrite(EC_PIN_RELAY_RANGE,LOW);
} }
void ec_loop(unsigned long loopmillis, unsigned long pInterval) { void ec_loop(unsigned long loopmillis) {
if (loopmillis>last_read_ec+pInterval) { static unsigned long last_measurement_ec=0;
last_read_ec=loopmillis; static unsigned long last_read_ec=0;
ec_array_pos++;
flag_print= ec_array_pos==EC_ARRAY_SIZE;
ec_array_pos%=EC_ARRAY_SIZE;
ec_array[ec_array_pos]=analogRead(EC_PIN_ADC);
//Serial.print(ec_array[ec_array_pos]); Serial.print(" "); 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);
Serial.print("EC ADC: Low="); Serial.print(adc_rangelow); Serial.print(" High="); Serial.println(adc_rangehigh);
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() {
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
digitalWrite(EC_PIN_RELAY_RANGE,LOW);
ec_last_change_relay=millis();
}
#endif #endif

View File

@ -19,8 +19,8 @@ void flow_setup() {
attachInterrupt(FLOW_PIN, isr_flow, CHANGE); attachInterrupt(FLOW_PIN, isr_flow, CHANGE);
} }
void flow_loop(unsigned long loopmillis, unsigned long pInterval) { void flow_loop(unsigned long loopmillis) {
if (loopmillis>=last_read_flow+pInterval) { if (loopmillis>=last_read_flow+READINTERVAL_FLOW) {
flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz] flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz]
flow/=flow_factor; //[L/min] flow/=flow_factor; //[L/min]

View File

@ -78,12 +78,12 @@ void temperature_setup() {
} }
void temperature_loop(unsigned long loopmillis, unsigned long pInterval) { void temperature_loop(unsigned long loopmillis) {
static unsigned long last_read_ds18b20; static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false; static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+pInterval) { if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
if (loopmillis>last_read_ds18b20+pInterval*10) { //timeout if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*10) { //timeout
Serial.println("Warn: Request Temperatures Timeout!"); Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false; flag_requestTemperatures=false;
} }

View File

@ -24,10 +24,10 @@ void waterlevel_setup() {
} }
} }
void waterlevel_loop(unsigned long loopmillis, unsigned long pInterval) { void waterlevel_loop(unsigned long loopmillis) {
static unsigned long last_read_hcsr04; static unsigned long last_read_hcsr04;
if (loopmillis>=last_read_hcsr04+pInterval) { if (loopmillis>=last_read_hcsr04+READINTERVAL_HCSR04) {
last_read_hcsr04=loopmillis; last_read_hcsr04=loopmillis;
float temperature=20.0; float temperature=20.0;
if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok

View File

@ -51,22 +51,24 @@ void loop() {
flag_print=false; flag_print=false;
ec_loop(loopmillis, EC_READ_INTERVAL); ec_loop(loopmillis);
temperature_loop(loopmillis, READINTERVAL_DS18B20); temperature_loop(loopmillis);
waterlevel_loop(loopmillis, READINTERVAL_HCSR04); waterlevel_loop(loopmillis);
flow_loop(loopmillis, READINTERVAL_FLOW); flow_loop(loopmillis);
if (loopmillis>last_print+500) { if (loopmillis>last_print+500) {
last_print=loopmillis; last_print=loopmillis;
/*
if (isValueArrayOK(ec_array,EC_ARRAY_SIZE,0)) if (isValueArrayOK(ec_array,EC_ARRAY_SIZE,0))
{ {
Serial.print("EC="); Serial.print("EC=");
@ -75,6 +77,7 @@ void loop() {
}else{ }else{
Serial.print("Waiting for EC"); Serial.print("Waiting for EC");
} }
if (isValueArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){ if (isValueArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){
Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE)); Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE));
@ -103,6 +106,7 @@ void loop() {
Serial.println(); Serial.println();
*/
} }