HI all,
I have recently bought a 'A02YYUW' [Waterproof Ultrasonic Sensor](A02YYUW-DFRobot Waterproof Ultrasonic Sensor SKU: SEN0311) to use in place of the waterproof ultra sonic sensor I bought from Amazon.
The one I bought off Amazon works jsut fine using the standard/beginners HC SR04 code you can find all over the internet -Random Nerd Tutorials (for example)
However the example code provided with the A02YYUW sensor is completely different and I'm sure if I just copied and pasted it, it would work just fine. I'm just a little unsure about the way it's all witten.
/*
*@File : DFRobot_Distance_A02.ino
*@Brief : This example use A02YYUW 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 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])
{
distance=(data[1]<<8)+data[2];
if(distance>30)
{
Serial.print("distance=");
Serial.print(distance/10);
Serial.println("cm");
}else
{
Serial.println("Below the lower limit");
}
}else Serial.println("ERROR");
}
delay(100);
}
I'm not sure what the do while loop is doing or mean?
What is the 0xff bit about?
And the if statement following the flush?
Pretty much the whole loop isn't anything like I've been used to since starting this Arduino journey. If anyone can help that would be apprecitated. Even just a hint that I should go away and read up on this, that or the other.
Thank you
Search tags:
A02YYUW
DYPA02YY