ArduinoGeneral

SEn0311 A02YYUW using 2 sensor at onces

userHead AlexanderTran 2022-06-25 03:15:47 486 Views1 Replies

Im trying to use 2 of those SEN0311 at once for one code program. can anyone help out? Im stuggling with the codes. Here is my current codes:

 

/*

*@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 A02YYUWsensorSerial(11,10); // RX1, TX1

SoftwareSerial A02YYUWsensorSerial1(6,5); // RX1, TX1

unsigned char data[4]={};

unsigned char data1[4]={};

float distance;

float distance1;

const int relay = 7;

const int relay1 = 4;

void setup()

{

Serial.begin(9600);

A02YYUWsensorSerial.begin(9600);

A02YYUWsensorSerial1.begin(9600);

pinMode(relay, OUTPUT);

pinMode(relay1, OUTPUT);

}


 

void loop()

{

do{

for(int i=0;i<4;i++)

{

data[i]=A02YYUWsensorSerial.read();

data1[i]=A02YYUWsensorSerial1.read();

}

}while(A02YYUWsensorSerial.read()==0xff && A02YYUWsensorSerial1.read()==0xff);


 

A02YYUWsensorSerial.flush();

A02YYUWsensorSerial1.flush();


 

if(data[0]==0xff && data1[0]==0xff)

{

int sum, sum1;

sum=(data[0]+data[1]+data[2])&0x00FF;

sum1=(data1[0]+data1[1]+data1[2])&0x00FF;

if(sum==data[3] && sum1==data[3])

{

distance=(data[1]<<8)+data[2];

distance1=(data1[1]<<8)+data1[2];

if(distance>50 && distance1>50)

{

Serial.print("Left side - distance=");

Serial.print(distance/10);

Serial.println("cm");

Serial.print(" Right side - distance=");

Serial.print(distance1/10);

Serial.println("cm");

digitalWrite(relay,LOW);

digitalWrite(relay1,LOW);

}

if (distance<50 && distance1>50)

{

Serial.println("Left side-Below the lower limit");

digitalWrite(relay,HIGH);

digitalWrite(relay1,LOW);

}

else if (distance>50 && distance1<50)

{

Serial.println("Right side -Below the lower limit");

digitalWrite(relay1,HIGH);

digitalWrite(relay,LOW);

}

else if (distance<50 && distance1<50)

{

Serial.println("Both side -Below the lower limit");

digitalWrite(relay1,HIGH);

digitalWrite(relay,HIGH);

}

else Serial.println("ERROR");

}

delay(100);

}

}

2023-03-03 14:39:45

Hi

You can not use two software serial at one time. 

This means that, unless you are using MCU like mega 2560 with multiple serial ports, you can not use two A02YYUW Waterproof Ultrasonic Sensors at one time.

Sorry for that.

userHeadPic NeloKin