added echo code

This commit is contained in:
Empire 2021-03-04 22:13:02 +01:00
parent 38ffd64305
commit 0784d2da44

View File

@ -31,6 +31,7 @@
* DEFINES * DEFINES
******************************************************************************/ ******************************************************************************/
#define AMOUNT_SENOR_QUERYS 8 #define AMOUNT_SENOR_QUERYS 8
#define MAX_TANK_DEPTH 1000
/****************************************************************************** /******************************************************************************
* FUNCTION PROTOTYPES * FUNCTION PROTOTYPES
@ -89,35 +90,6 @@ Plant mPlants[MAX_PLANTS] = {
/****************************************************************************** /******************************************************************************
* LOCAL FUNCTIONS * LOCAL FUNCTIONS
******************************************************************************/ ******************************************************************************/
long getDistance()
{
byte startByte, h_data, l_data, sum;
byte buf[3];
startByte = (byte)Serial.read();
if (startByte == 255)
{
unsigned int distance;
Serial.readBytes(buf, 3);
h_data = buf[0];
l_data = buf[1];
sum = buf[2];
distance = (h_data << 8) + l_data;
if (((startByte + h_data + l_data) & 0xFF) != sum)
{
return -1;
}
else
{
return distance;
}
}
else
{
return -2;
}
}
long getCurrentTime() long getCurrentTime()
{ {
struct timeval tv_now; struct timeval tv_now;
@ -284,22 +256,22 @@ void mode2MQTT()
*/ */
void readDistance() void readDistance()
{ {
for (int i = 0; i < 5; i++) for (int i = 0; i < AMOUNT_SENOR_QUERYS; i++)
{ {
long start = millis(); unsigned long duration = 0;
while (!Serial.available())
{ digitalWrite(triggerPin, HIGH);
if ((start + 500) < millis()) delayMicroseconds(20);
{ cli();
Serial << "Abort reading hall sensor, not measurement after 200ms" << endl; digitalWrite(triggerPin, LOW);
waterRawSensor.add(0); duration = pulseIn(echoPin, HIGH);
return; sei();
}
} int mmDis = duration * 0.3432 / 2;
unsigned int distance = getDistance(); if(mmDis > MAX_TANK_DEPTH){
if (distance > 0) waterRawSensor.add(0);
{ } else {
waterRawSensor.add(distance); waterRawSensor.add(mmDis);
} }
} }
} }