Here's my code. This is installed on Arduino Uno 1.
String command = "";
void setup() {
Serial.begin(9600);
while (!Serial) { ; }
Serial.println("Starting....");
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if ( c == '\n' ) { command = ""; }
else
{ command += c; }
}
if ( command == "ABC" ) {
Serial.println("You typed: " + command);
delay(500);
}
}
When I run this and run the serial monitor, it works fine. I type 'ABC', hit send, and it says, 'You typed: ABC"
The problem comes when I try to hook this arduino up to another one. I plug in arduino Uno 2 and connect it to Arduino Uno 1 (with the above code)
RX to RX, and TX to TX.
When I use the Serial Monitor now ( which is connected to Arduino 2 ), the first line works, it says, 'Starting...' so I know I'm getting something from Arduino 1.
The problem is when I type something in the Serial Monitor and hit send I get no response. It seems that Serial.Available() is coming up 0.
It is working exactly the way it should. Rx is receiver, think of your TV You can watch it but it does not send your picture anywhere because it is a receiver. Not think of a wireless microphone you can talk into it and it will be received on one of the appropriately tuned receiver but it will not speak at you. You need Tx -> Rx and Rx -> Tx. Be sure both are at the same baud! Happy computing. You might spend a little time researching RS232 signals.
The above connection is not allowed practically. If you do so, none of the UNOs would work and in my case they don't work at all even the sketches are loaded into them.
Let me explain to you (with the help of Fig-1) what happens when you execute the following code:
char y = Serial.read();
1. When we enter ABC in the InputBox of the Serial Monitor, the ASCII codes pf these characters (0x41, 0x42, 0x43) arrive to UNO (one-after-another) via RXD-pin and are saved into the Serial Buffer on interrupt Basis.
2. The execution of char y = Serial.read() command brings out 0x41 from TOP position (Location-0) of the Buffer into variable y. The data byte x41 takes the position of Location-0 and 0x42 takes the position of Location-1. The Serial Buffer is a first-in first-out (FIFO) type buffer.
3. Concepts similar to the above may also be applied to understand the functioning of Serial.write(arg) command.
Must say I'm confused about the connections too. Are they both on usbs in different instances of the monitor on different com ports or what. Perhaps @studiogamma could drop a diagram in here to clarify.
But anyway, I would think that if you want serial comms between 2 Arduinos then software serial would be the thing to use, and leave the hardware serial on the usb intact for program loading and the monitor, with no chance of this confusion.
That's irrelevant on an Uno where Serial is always true.
Uno 2 is connected to the computer via USB ( so it uses the serial monitor ) . It’s connected to Uno 1 via RX to RX and TX to TX and Gnd to gnd. Uno 1 is not connected to a computer. The shown code is on the Uno 1.
It does display ‘Starting…’ , which goes from the 1 to the 2 and displays on the 2’s serial monitor. It just seems to hang up on Serial.available() on Uno 1when I try to send data back (serial monitor to Usb2 to Usb1)
@studiogamma you should look at the software serial library for the Uno-Uno comms. It installs in the IDE as part of the Arduino install if I recall, and there is an example or two.
Then on each Uno you stick this in your code, using whatever pins you like other than 0 or 1:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX (call it what you like, mySerial is an example)
.... and join the Unos together on those pins, crossed over R-T and T-R.
Then, when you want to communicate between them you use mySerial. rather than Serial. which is still then used for Uno-serial monitor comms.