A01NYUB Waterproof Ultrasonic Sensor Code Displaying Random Letters And Numbers

Hello everyone,

I am working on a project with an ultrasonic sensor and I just upgraded to this new senor and cannot seem to get the code to work as the monitor is just displaying random numbers and letters and I cannot seem to figure it out. This is a link to the site with information about the sensor, [A01NYUB Waterproof Ultrasonic Sensor Wiki - DFRobot](A01NYUB-DFRobot Waterproof Ultrasonic Sensor SKU: SEN0313).

#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>280)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
              {
                Serial.println("Below the lower limit");        
              }
      }else Serial.println("ERROR");
     }
     delay(150);
}

If anyone has any ideas as to why this is happening that would be awesome thanks! Also I have my serial monitor set to 9600.

jacobroberts08:
.... I have my serial monitor set to 9600.

Serial.begin(57600);

Oops!!!

I actually tried that as well and it still kept printing random letters and numbers.

The first thing you do in loop is read four characters that may not have arrived yet.

Why?

Good question, honestly I found this code off of their website and I am trying to debug it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.