Hello
I am facing a problem in storing data from serial into a variable
What I am trying to do is to receive two signals from two serials (Serial1 and Serial2) and then I would like to compare the two received signals and based on the comparison I will retransmit the signal again.
I tested this by sending one signal twice.
The receiving code:
int x, y;
void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
Serial1.begin(9600);// pin 19(Rx), 18(Tx)
Serial2.begin(9600);// pin 17(Rx), 16(Tx)
}
void loop() {
if (Serial1.available()) {
x = Serial1.read();
Serial.write(x);
//Serial1.write(x);
}
if (Serial.available()) {
Serial1.write(Serial.read());
}
if (Serial2.available()) {
y = Serial2.read();
Serial.write(y);
if (x==y)//This statment never holds true (although the signals are identical) but because x now does not hold the prevoius serial read
Serial2.write(y);
}
if (Serial.available()) {
Serial2.write(Serial.read());
}
}
So to sum up, based on my observation I found that once the program is outside the if-statement for serial1, the variable x never holds the received value.
I am seeking your kind help, I have read several posts and documents about serial UART but none uses x outside the serial1 block of the code. Is there any other way to that?
Thanks,