hydroponic-controller/include/waterlevel.h

44 lines
1.2 KiB
C

#ifndef _WATERLEVEL_H_
#define _WATERLEVEL_H_
#include <HCSR04.h>
#define HCSR04_PIN_ECHO 17
#define HCSR04_PIN_TRIGGER 16
#define HCSR04_TIMEOUT 5000 //default is 100000 (uS)
#define READINTERVAL_HCSR04 100
#define WATERLEVELMEAN_SIZE 32
float waterlevelMean[WATERLEVELMEAN_SIZE];
uint16_t waterlevelMean_pos=0;
void waterlevel_setup() {
//HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO);
HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO,HCSR04_TIMEOUT, HCSR04.eUltraSonicUnlock_t::unlockSkip);
for (uint16_t i=0;i<WATERLEVELMEAN_SIZE;i++) {
waterlevelMean[i]=-1; //-1 is also timeout value
}
}
void waterlevel_loop(unsigned long loopmillis, unsigned long pInterval) {
static unsigned long last_read_hcsr04;
if (loopmillis>=last_read_hcsr04+pInterval) {
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;
}
}
#endif