I am trying to read data from two sensors connected to rx1,tx1 and rx2,tx2. In the setu function I use Serial1 and Serial2. The problem I am facing is that sensor data of 1 sensor changes whereas the sensor data of the other sensor does not change.It keeps on printing the same data again and again. The sensor works well when tested individuallly. The problem arises only when I use two rx and tx pins and use two sensors.
Should I introduce a wait time to print the data from second sensor or should I enable the second serial port?
Should I introduce a wait time to print the data from second sensor or should I enable the second serial port?
Code:
#include "Wire.h"
#define XSERIAL Serial1
#define YESERIAL Serial2
void setup() {
Serial.begin(115200);
XSERIAL.begin(115200, SERIAL_8N1);
YSERIAL.begin(115200, SERIAL_8N1);
}
void loop() {
int RxMsg;
unsigned int bytec = 0;
unsigned char Data[246];
int RxMsg1;
unsigned int byte1 = 0;
unsigned char Data1[246];
Serial.println();
Serial.print("NEW DATA");
Serial.println();
delay(1);
while(XSERIAL.available() && bytec < 544)
{
RxMsg = XSERIAL.read();
Data[bytec] = RxMsg;
bytec++;
delayMicroseconds(90);
}
delay(100);
Serial.println();
Serial.clear();
XSERIAL.clear();
for(int i = 0; i < 121; i++)
{
Serial.print("data 1: ");
Serial.print(Data[i]);
Serial.print(", ");
Serial.print('\n');
}
delay(1);
while(YSERIAL.available() && byte1 < 544)
{
RxMsg1 = YSERIAL.read();
Data1[byte1] = RxMsg1;
byte1++;
}
delay(100);
Serial.println();
Serial.clear();
YSERIAL.clear();
for(int i = 0; i < 121; i++)
{
Serial.print("data2: ");
Serial.print(Data1[i]);
Serial.print(", ");
}
Serial.println();
Serial.println();
}