From 390aaeaeb71e2a0cb992c2276f02a9376b38fe09 Mon Sep 17 00:00:00 2001 From: Fisch Date: Fri, 28 Apr 2023 15:07:47 +0200 Subject: [PATCH] remove range switching --- include/README | 39 ----------------------- include/ec.h | 64 +++++++------------------------------ src/main.cpp | 86 ++++++++++++++++++++++++-------------------------- 3 files changed, 53 insertions(+), 136 deletions(-) delete mode 100644 include/README diff --git a/include/README b/include/README deleted file mode 100644 index 194dcd4..0000000 --- a/include/README +++ /dev/null @@ -1,39 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the usual convention is to give header files names that end with `.h'. -It is most portable to use only letters, digits, dashes, and underscores in -header file names, and at most one dot. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/ec.h b/include/ec.h index fb695d6..a4615a6 100644 --- a/include/ec.h +++ b/include/ec.h @@ -8,8 +8,6 @@ float ecEC_ADS_CHANNEL_mean=0; bool ec_flag_measurement_available=false; #define EC_PIN_RELAY_PROBE 27 -#define EC_PIN_RELAY_RANGEMSB 25 -#define EC_PIN_RELAY_RANGELSB 26 //#define EC_PIN_ADC 4 @@ -20,11 +18,8 @@ bool ec_flag_measurement_available=false; #define EC_FREQUENCY 5000 #define EC_ARRAY_SIZE 64 -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; +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 @@ -40,11 +35,7 @@ enum ECState{IDLE,MEASURE}; ECState ecstate=IDLE; -float adc_range00; -float adc_range01; -float adc_range10; -float adc_range11; - +float ec_adc; bool ec_measurementReady(); void ec_startMeasurement(); @@ -60,8 +51,6 @@ 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_RANGELSB,OUTPUT); //LOW=NC, HIGH=NO - pinMode(EC_PIN_RELAY_RANGEMSB,OUTPUT); //LOW=NC, HIGH=NO ec_releaseRelay(); } @@ -77,6 +66,7 @@ void ec_loop(unsigned long loopmillis) { last_measurement_ec=loopmillis; ec_startMeasurement(); ec_connectProbe(true); + Serial.println("Measuring EC"); ecstate=MEASURE; } break; @@ -84,13 +74,9 @@ void ec_loop(unsigned long loopmillis) { case MEASURE: if (ec_measurementReady()) { ec_releaseRelay(); - 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 + Serial.println("Finished EC"); + ec_adc=getMean(ec_array,EC_ARRAY_SIZE); - - //ec_mean=0; //TODO select right range of all readings ec_flag_measurement_available=true; @@ -106,36 +92,21 @@ void ec_loop(unsigned long loopmillis) { - if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/4last_read_ec+EC_READ_INTERVAL && ec_array_posec_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); - 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; - } + + ec_array[ec_array_pos]=value; + ec_array_pos++; } @@ -151,24 +122,13 @@ void ec_startMeasurement() { } bool ec_measurementReady(){ - if (ec_array_pos/EC_ARRAY_SIZE>=4) { //reached end of both arrays + if (ec_array_pos>=EC_ARRAY_SIZE) { //reached end of both arrays return true; }else{ return false; } } -void ec_setRange(uint8_t range) { - //range low means low resistor value -> NO -> relay High - - 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); - ec_last_change_relay=millis(); - } -} void ec_connectProbe(bool relay) { bool val=digitalRead(EC_PIN_RELAY_PROBE); @@ -180,8 +140,6 @@ void ec_connectProbe(bool relay) { void ec_releaseRelay() { digitalWrite(EC_PIN_RELAY_PROBE,LOW); - digitalWrite(EC_PIN_RELAY_RANGEMSB,LOW); - digitalWrite(EC_PIN_RELAY_RANGELSB,LOW); ec_last_change_relay=millis(); } diff --git a/src/main.cpp b/src/main.cpp index 2e3427d..a792cb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,7 +61,7 @@ void setup() { //Serial.println("Setup finished"); delay(200); - Serial.println("time,tempReservoir,EC00,EC01,EC10,EC11"); + Serial.println("time,tempReservoir,EC"); } @@ -95,58 +95,56 @@ void loop() { } - if (loopmillis>last_print+10000 && loopmillis>60000) { + if (loopmillis>last_print+2000) { //if (ec_flag_measurement_available && getReading) { - last_print=loopmillis; - getReading=false; - ec_flag_measurement_available=false; - digitalWrite(PIN_LED,LOW); + if (ec_flag_measurement_available) { + last_print=loopmillis; + getReading=false; + ec_flag_measurement_available=false; + digitalWrite(PIN_LED,LOW); - Serial.print(millis()/1000.0,2); Serial.print(","); - Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(","); - //Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(","); - - Serial.print(adc_range00); Serial.print(","); - Serial.print(adc_range01); Serial.print(","); - Serial.print(adc_range10); Serial.print(","); - Serial.print(adc_range11); - Serial.println(); + Serial.print(millis()/1000.0,2); Serial.print(","); + Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print(","); + //Serial.print(getMean(sm_mean,SM_SIZE)); Serial.print(","); + + Serial.print(ec_adc); + Serial.println(); - /* - - - 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)); - }else{ - Serial.print("\t waiting for temperature"); - } - - - if (isValueArrayOKf(waterlevelMean,WATERLEVELMEAN_SIZE,-1.0)){ - float _max=getMaxf(waterlevelMean,WATERLEVELMEAN_SIZE); - float _min=getMinf(waterlevelMean,WATERLEVELMEAN_SIZE); - - float _filteredWaterlevel=getFilteredf(waterlevelMean,WATERLEVELMEAN_SIZE,8); - float _meanWaterlevel=getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE); + /* - Serial.print("\t Dist="); Serial.print(_filteredWaterlevel); Serial.print("mm"); Serial.print("(+- "); Serial.print((_max-_min)/2.0); Serial.print(")"); Serial.print(" [mean="); Serial.print(_meanWaterlevel); Serial.print("]"); + 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)); + }else{ + Serial.print("\t waiting for temperature"); + } + + + if (isValueArrayOKf(waterlevelMean,WATERLEVELMEAN_SIZE,-1.0)){ + float _max=getMaxf(waterlevelMean,WATERLEVELMEAN_SIZE); + float _min=getMinf(waterlevelMean,WATERLEVELMEAN_SIZE); + + float _filteredWaterlevel=getFilteredf(waterlevelMean,WATERLEVELMEAN_SIZE,8); + float _meanWaterlevel=getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE); + + + Serial.print("\t Dist="); Serial.print(_filteredWaterlevel); Serial.print("mm"); Serial.print("(+- "); Serial.print((_max-_min)/2.0); Serial.print(")"); Serial.print(" [mean="); Serial.print(_meanWaterlevel); Serial.print("]"); + + }else{ + Serial.print("\t waiting for distance"); + } + + Serial.print("\t Flow="); Serial.print(flow,2); + Serial.print("\t Flowsum="); Serial.print(flow_counter_sum); + + + + Serial.println(); + */ - }else{ - Serial.print("\t waiting for distance"); } - - Serial.print("\t Flow="); Serial.print(flow,2); - Serial.print("\t Flowsum="); Serial.print(flow_counter_sum); - - - - Serial.println(); - */ - - //} } }