add hc-sr04 ultrasonic distance sensor
This commit is contained in:
parent
ca7ccc023d
commit
c90e106ba6
|
@ -16,4 +16,5 @@ framework = arduino
|
|||
monitor_speed = 115200
|
||||
|
||||
lib_deps =
|
||||
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
||||
https://github.com/milesburton/Arduino-Temperature-Control-Library/
|
||||
d03n3rfr1tz3/HC-SR04@^1.1.2
|
58
src/main.cpp
58
src/main.cpp
|
@ -44,7 +44,15 @@ float tempC_air;
|
|||
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;
|
||||
|
@ -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;i<TEMPMEAN_SIZE;i++) {
|
||||
tempCmean_reservoir[i]=-127;
|
||||
|
@ -126,6 +136,8 @@ void loop() {
|
|||
|
||||
bool flag_print=false;
|
||||
|
||||
|
||||
|
||||
if (loopmillis>last_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<psize;i++) {
|
||||
if (parray[i]==pcheck){
|
||||
return false;
|
||||
|
@ -219,7 +259,7 @@ bool isTempArrayOK(uint16_t *parray,uint16_t psize, uint16_t pcheck) { //check i
|
|||
}
|
||||
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++) {
|
||||
if (parray[i]==pcheck){
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue