add hc-sr04 ultrasonic distance sensor

This commit is contained in:
interfisch 2023-04-05 09:22:57 +02:00
parent ca7ccc023d
commit c90e106ba6
2 changed files with 51 additions and 10 deletions

View File

@ -16,4 +16,5 @@ framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = lib_deps =
https://github.com/milesburton/Arduino-Temperature-Control-Library/ https://github.com/milesburton/Arduino-Temperature-Control-Library/
d03n3rfr1tz3/HC-SR04@^1.1.2

View File

@ -44,7 +44,15 @@ float tempC_air;
float tempCmean_air[TEMPMEAN_SIZE]; float tempCmean_air[TEMPMEAN_SIZE];
// ######## Water Level
#include <HCSR04.h>
#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; unsigned long last_print=0;
@ -55,8 +63,8 @@ float getMean(uint16_t *parray,uint16_t psize);
float getMeanf(float *parray,uint16_t psize); float getMeanf(float *parray,uint16_t psize);
uint16_t getMin(uint16_t *parray, uint16_t psize); uint16_t getMin(uint16_t *parray, uint16_t psize);
uint16_t getMax(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 isValueArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck);
bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck); bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck);
void printAddress(DeviceAddress deviceAddress); void printAddress(DeviceAddress deviceAddress);
@ -75,6 +83,8 @@ void setup() {
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH); ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127); ledcWrite(EC_PWM_CH, 127);
HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO);
//initialize mean array //initialize mean array
for (uint16_t i=0;i<TEMPMEAN_SIZE;i++) { for (uint16_t i=0;i<TEMPMEAN_SIZE;i++) {
tempCmean_reservoir[i]=-127; tempCmean_reservoir[i]=-127;
@ -126,6 +136,8 @@ void loop() {
bool flag_print=false; bool flag_print=false;
if (loopmillis>last_read_ec+EC_READ_INTERVAL) { if (loopmillis>last_read_ec+EC_READ_INTERVAL) {
last_read_ec=loopmillis; last_read_ec=loopmillis;
ec_array_pos++; ec_array_pos++;
@ -135,6 +147,7 @@ void loop() {
//Serial.print(ec_array[ec_array_pos]); Serial.print(" "); //Serial.print(ec_array[ec_array_pos]); Serial.print(" ");
} }
static unsigned long last_read_ds18b20; static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false; static bool flag_requestTemperatures=false;
@ -144,7 +157,7 @@ void loop() {
flag_requestTemperatures=false; flag_requestTemperatures=false;
} }
if (!flag_requestTemperatures) { if (!flag_requestTemperatures) {
sensors.requestTemperatures(); sensors.requestTemperatures(); //this takes ~600ms
flag_requestTemperatures=true; flag_requestTemperatures=true;
} }
if (sensors.isConversionComplete()) { if (sensors.isConversionComplete()) {
@ -171,9 +184,28 @@ void loop() {
tempCmean_pos%=TEMPMEAN_SIZE; 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) { if (loopmillis>last_print+500) {
last_print=loopmillis; last_print=loopmillis;
@ -181,11 +213,19 @@ void loop() {
Serial.print(getMean(ec_array,EC_ARRAY_SIZE),3); 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)); 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)); Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE));
}else{ }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(); Serial.println();
@ -211,7 +251,7 @@ float getMeanf(float *parray,uint16_t psize) {
return mean/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<psize;i++) { for (uint16_t i=0;i<psize;i++) {
if (parray[i]==pcheck){ if (parray[i]==pcheck){
return false; return false;
@ -219,7 +259,7 @@ bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck) { //check i
} }
return true; return true;
} }
bool isTempArrayOKf(float *parray,uint16_t psize, float pcheck) { //check if array has error values bool isValueArrayOKf(float *parray,uint16_t psize, float pcheck) { //check if array has error values
for (uint16_t i=0;i<psize;i++) { for (uint16_t i=0;i<psize;i++) {
if (parray[i]==pcheck){ if (parray[i]==pcheck){
return false; return false;