Dear all,
I have an issue using the A01NYUB US distance sensor. It communicates via UART with my arduino and I can print the serial output ('value' var). There is only one problem (see handle() function at the bottom), when I try to evaluate the float (value variable I pass to this function), and print a warning text to serial monitor, always the same integer ('11822') is printed instead of the warning message... (screenshot of serial monitor output attached)
Below is the modified code example I got from dfrobot.com:
/*
*@File : DFRobot_Distance_A01.ino
*@Brief : This example use A01NYUB ultrasonic sensor to measure distance
* With initialization completed, We can get distance value
*@Copyright [DFRobot](https://www.dfrobot.com),2016
* GUN Lesser General Pulic License
*@version V1.0
*@data 2019-8-28
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float value;
float distance;
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop()
{
do{
for(int i=0;i<4;i++)
{
data[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(data[0]==0xff)
{
int sum;
sum=(data[0]+data[1]+data[2])&0x00FF;
if(sum==data[3])
{
value=(data[1]<<8)+data[2];
handle(value);
}
else
{
Serial.println("ERROR");
}
}
delay(150);
}
char handle(float value)
{
if(value>280)
{
distance = value/10;
Serial.print(distance);
Serial.println("cm");
if(distance<30)
{
Serial.println('High water');
}
else if(distance>=30 and distance<40)
{
Serial.println('Water is rising, attention!');
}
else
{
Serial.println('All Safe...');
}
}
else
{
Serial.println("Below the lower limit");
}
}

Many thanks in advance for your help!
