I'm using a MKR wifi 1010 for a project and want to code the ultrasonic sensor A02YYUW . i found a code on the forum and changed the softwareserial.h to Serial1 so it works with my board (attached below). But i only keep getting Error in my serial monitor and not a reading. Any solution ?
unsigned char data[4]={};
float distance;
void setup()
{
Serial.begin(57600);
Serial1.begin(9600);
}
void loop()
{ do{
for(int i=0;i<4;i++)
{
data[i]=Serial1.read();
}
}while(Serial1.read()==0xff);
Serial1.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);
}