From c90e106ba6422598ce30414e196a4320327a8856 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 5 Apr 2023 09:22:57 +0200 Subject: [PATCH] add hc-sr04 ultrasonic distance sensor --- platformio.ini | 3 ++- src/main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/platformio.ini b/platformio.ini index 0c55a85..02ecd40 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,4 +16,5 @@ framework = arduino monitor_speed = 115200 lib_deps = - https://github.com/milesburton/Arduino-Temperature-Control-Library/ \ No newline at end of file + https://github.com/milesburton/Arduino-Temperature-Control-Library/ + d03n3rfr1tz3/HC-SR04@^1.1.2 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 485b09a..eb46c13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,7 +44,15 @@ float tempC_air; float tempCmean_air[TEMPMEAN_SIZE]; - +// ######## Water Level +#include +#define HCSR04_PIN_ECHO 17 +#define HCSR04_PIN_TRIGGER 16 +#define READINTERVAL_HCSR04 100 + +#define WATERLEVELMEAN_SIZE 32 +float waterlevelMean[WATERLEVELMEAN_SIZE]; +uint16_t waterlevelMean_pos=0; unsigned long last_print=0; @@ -55,8 +63,8 @@ float getMean(uint16_t *parray,uint16_t psize); float getMeanf(float *parray,uint16_t psize); uint16_t getMin(uint16_t *parray, uint16_t psize); uint16_t getMax(uint16_t *parray, uint16_t psize); -bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); -bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck); +bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck); +bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck); void printAddress(DeviceAddress deviceAddress); @@ -75,6 +83,8 @@ void setup() { ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcWrite(EC_PWM_CH, 127); + HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO); + //initialize mean array for (uint16_t i=0;ilast_read_ec+EC_READ_INTERVAL) { last_read_ec=loopmillis; ec_array_pos++; @@ -135,6 +147,7 @@ void loop() { //Serial.print(ec_array[ec_array_pos]); Serial.print(" "); } + static unsigned long last_read_ds18b20; static bool flag_requestTemperatures=false; @@ -144,7 +157,7 @@ void loop() { flag_requestTemperatures=false; } if (!flag_requestTemperatures) { - sensors.requestTemperatures(); + sensors.requestTemperatures(); //this takes ~600ms flag_requestTemperatures=true; } if (sensors.isConversionComplete()) { @@ -171,9 +184,28 @@ void loop() { tempCmean_pos%=TEMPMEAN_SIZE; } - } + + + + static unsigned long last_read_hcsr04; + if (loopmillis>=last_read_hcsr04+READINTERVAL_HCSR04) { + last_read_hcsr04=loopmillis; + float temperature=20.0; + if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok + temperature=getMeanf(tempCmean_air,TEMPMEAN_SIZE); + } + + + double* distances = HCSR04.measureDistanceMm(temperature); + + waterlevelMean[waterlevelMean_pos]=distances[0]; + waterlevelMean_pos++; + waterlevelMean_pos%=WATERLEVELMEAN_SIZE; + } + + if (loopmillis>last_print+500) { last_print=loopmillis; @@ -181,11 +213,19 @@ void loop() { Serial.print(getMean(ec_array,EC_ARRAY_SIZE),3); Serial.print("\t spread="); Serial.print(getMax(ec_array,EC_ARRAY_SIZE) - getMin(ec_array,EC_ARRAY_SIZE)); - if (isTempArrayOKf(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)); }else{ - Serial.print("\t waiting for temperature array"); + Serial.print("\t waiting for temperature"); } + + + if (isValueArrayOKf(waterlevelMean,WATERLEVELMEAN_SIZE,0)){ + Serial.print("\t Dist="); Serial.print(getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE)); Serial.print("mm"); + }else{ + Serial.print("\t waiting for distance"); + } + Serial.println(); @@ -211,7 +251,7 @@ float getMeanf(float *parray,uint16_t psize) { return mean/psize; } -bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck) { //check if array has error values +bool isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck) { //check if array has error values for (uint16_t i=0;i