added echo code
This commit is contained in:
parent
38ffd64305
commit
0784d2da44
@ -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);
|
||||||
|
duration = pulseIn(echoPin, HIGH);
|
||||||
|
sei();
|
||||||
|
|
||||||
|
int mmDis = duration * 0.3432 / 2;
|
||||||
|
if(mmDis > MAX_TANK_DEPTH){
|
||||||
waterRawSensor.add(0);
|
waterRawSensor.add(0);
|
||||||
return;
|
} else {
|
||||||
}
|
waterRawSensor.add(mmDis);
|
||||||
}
|
|
||||||
unsigned int distance = getDistance();
|
|
||||||
if (distance > 0)
|
|
||||||
{
|
|
||||||
waterRawSensor.add(distance);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user