I am trying to do a loopback test using an Arduino Mega. What I am looking to do is send a byte over TX and receive said byte back at RX and verify that the byte is unchanged. More or less I just have a jumper wire connecting the TX and RX ports (In reality I have a circuit hooked up to the TX RX ports that effectively acts like a wire, I'm just trying to verify that the circuit traces are sound).
I am also doing the exact same thing over TX2/RX2 and TX3/RX3.
The issue I have run into is that I can't seem to get the data to send out the TX ports. In my attempt I used Serial objects where I would do the following:
void setup() {
Serial.begin(9600); //0 and 1
Serial2.begin(9600); //16 and 17
Serial3.begin(9600); //14 and 15
}
void loop() {
//RX TX
int bytesSent = Serial.write(1);
delay(500);
if(Serial.read()!=bytesSent){
//Do Something
}
delay(500);
//RX2 TX2
bytesSent = Serial2.write(2);
delay(500);
if(Serial2.read()!=bytesSent){
//Do Something
}
delay(500);
//RX3 TX3
bytesSent = Serial3.write(3);
delay(500);
if(Serial3.read()!=bytesSent){
//Do Something
}
delay(500);
}
I'm aware that the use of "delay(500)" is not optimal nor good coding practice but is left here for now while I'm still trying to understand what's wrong with my transmit and receive.
I believe my misunderstanding comes in with the Serial objects; Am I not using them correctly in this case? I've referenced the Arduino Serial reference guide, so this seems like it would work, but does not.
Further details: I am currently powering my Arduino via USB (which I'm aware can be an issue, but I currently do not have the battery power source). Would switching to the other power source resolve my issue? Just to reiterate I'm not trying to communicate with the PC, I'm just trying to verify that this external circuit (which acts like a wire) is able to carry the signal.
bytesSent
contains the data that was sent