Problem Connecting Arduinos Over Serial

I'm running some tests so that I can ultimately connect two Arduinos with those really cheap wireless modules. I've been paring down my code all day until getting to the bare bones you see here to isolate my problems. Right now I have an Arduino Pro (5V/16MHz) as the "transmitter" wired directly from pin 1 to pin 0 on a Duemilanove. The Pro is running this code:

byte counter;
void setup() {
  Serial.begin(1200);
  counter = 0;
}
void loop() {
  Serial.write(counter);
  counter++;
  delay(10);
}

When I watch it on the console through my FTDI Basic it looks like it's working. So, I load the following code on the Duemilanove:

byte incoming = 0;
void setup() {
  Serial.begin(1200);
}
void loop() {
  if (Serial.available() > 0) {
    //Serial.print("Serial Available\t");
    incoming = Serial.read();
    Serial.println(incoming, DEC);
    //Serial.flush();
  }
  //Serial.println("Waiting");
}

and nothing happens. Nothing at all ever, at least not that I can see on the console. You see I have placed various little things I have tried for troubleshooting there after comment slashes. When I uncomment the "Waiting" code it will tell me it's waiting and print numbers. But the numbers don't look right (they are usually about 190 to 250 or so but vary randomly). So in case it was too fast, I tried increasing the transmitter code's loop delay to 100ms, but to no avail. So I set the system up to not have the USB cable plugged in (used a 9V battery for power instead), in case it's hogging the serial line somehow, and that doesn't seem to work either.

I've tried a bunch of troubleshooting tricks, too many to describe here up to and including Serial.write() instead of Serial.print(), and it still doesn't work as expected. I've looked through the forums and haven't seen what seemed to be a similar problem either.

Note that I unplug the serial wires before I load the program. And I do have a common ground between the two Arduinos (and even to the FTDI Basic breakout when I use it).

I'm hoping I'm just doing something silly and a more experienced person can straighten me out in an instant. We'll see. And thanks for your help.

Can you draw a block diagram of how you have hardware connected? (Illustrating which TX and RXs are going where.)

a) Use NewSoftSerial so that your serial link can use pins other than 0 and 1, leaving them free and uncompromised for serial monitor and programming task.

b) Read....

... for various things about connecting Arduinos to things... even other Arduinos... by serial links.

I'll read through that link. I tried SoftwareSerial a little bit (not NewSoftSerial, though). I might also just try a different board. The problem is I don't have any more Arduinos right now (not 5V ones at least).

I put a sketch of the wiring over here: http://squishyrobot.files.wordpress.com/2011/10/img_20111002_182620.jpg

Note that I'm using power off the Duemilanove to power the Pro because at times I would remove the FTDI board to see if it works. I connected it to Vin on the Pro because it didn't seem to work on Vcc (I've done so many things it's hard to recall anymore).

So here's what I did:

  1. Load up the "receiver" code from my previous post.
  2. Set serial to COM3 and set the board to the Duemilanove.
  3. Upload the program.
  4. Load the "transmitter" code.
  5. Set serial to COM4 and set the board to the Arduino Pro 5V/16MHz.
  6. Upload the program.
  7. Open the serial console (we're still on COM4) and make sure it's listening at 1200.
  8. Observe a serial stream of ASCII characters from 0 to 255 over and over again.
  9. Change to COM3 and load up the serial console again (this should be the Duemilanove now).
  10. Absolutely nothing happens.
  11. Uncomment the "Waiting" line, reload (making sure to set the right COM port and Board).
  12. Observe "192Waiting" over and over again on the console. (The actual number will vary from line to line.)

This looks almost like it might be right, but it doesn't show all the numbers, even if I cut the transmission time to one byte every 100ms. It looks like it might be garbage numbers because it stays up near 190-250 or so.

Keep in mind that if you Serial.write(), binary numbers are sent. Unless those are in the range of 32-127, the serial monitor won't have anything to print.

I've tried Serial.print() as well. I must say it's not very scientific at this point because I've tried everything every way but without clean documentation. That said, with Serial.write() or Serial.print(), the console just tries to interpret it as ASCII anyway and gives its best guess, which I can usually interpret well enough to see if it's doing something roughly correct. So the first few are blank, but then it goes through punctuation, letters, numbers, special letters, and so on. Also, at first I formatted it as Serial.println(counter, DEC); just to make sure it was working before I went to sending pure bytes.

I'm wondering if the Arduino does some shenanigans with its serial port when it's being used over USB. I know it resets when I load up the console, so does whatever causes that cause me problems here? I don't have another Pro right now to test it.

Pins 0 and 1 are connected to the serial-to-USB chip so can't (readily) be used for anything else. You can connect the TX pin to another receiver which shares what the serial console sees, but you cannot in general send stuff to the RX pin because its being written to by the USB/serial chip. This is an area where boards differ (some have resistors to allow this to sort of work I think).

Use NewSoftSerial on the Duemilanove on pins other than 0 and 1, connect the Pro Mini's TX to the software RX pin you've chosen.

coherent.noise:
But the numbers don't look right (they are usually about 190 to 250 or so but vary randomly).

I can't really reproduce that. Using your exact posted code I get this:

2
5
9
12
15
19
22
25
29
32
35
39
42
45
49
52
55
59
62
65
69
72
76
80
85
89
93
97
101
105
110
114
118
122
126
130
135
139
143
147
151
155
160
164
168
172
176
180
185
189
193
197
201
205
210
214
218
222
226
230
234
239
243
247
250
252
255
3
6
...

Now certainly around every 3rd number is missing, but I would expect that. Why? To print an incoming byte (eg. with value 52) it takes 3 bytes ("5", "2", newline). So your debugging is consuming 3 character-times per incoming character. So you miss 2 out of 3 of them.

I suggest getting better debugging. For example a logic analyzer. Or see my post here:

That shows how you can debug serial comms by using SPI to another processor. SPI is much faster than serial, so you won't suffer from this problem.

Did you connect the grounds of the two boards ?

I did, yes. And I notice that the OP had Vin connected to 5V. Personally I just connected 5V to 5V (and Gnd to Gnd).

Regarding the problem of serial. "trying to interpret... as ASCII..." there IS a way to fix this, which from the list of data received I gather you found, but for others, I think the following is the answer, though I post this somewhat hastily...

If you want to print a number, put it in an "int" type variable, and use a separate line to print it, putting any text to go with the number in a separate Serial.print (or Serial.println) statement.

(If you have something in a byte type variable, say bMyNumber, you can even print THAT, but you must use the conversion function "int"... Your code would look like....

Serial.println(int(bMyNumber));

I got it working, more or less, when I switched the code loaded on the Pro and the Duemilanove so that I could unplug the FTDI Basic breakout from the programming header and just use it straight with a ground and serial in. (That is, I used the Pro instead of the Duemilanove as the device that was both sending and receiving.) It's quite inconvenient, of course, to have to unplug and replug the programming cable while debugging, so I'll try NewSoftSerial to communicate between the boards and the normal serial to go to and from the computer.

I say I got it working "more or less" because when I tried to re-complicate my code it stopped working. But the bare bones serial echo worked finally. I'll work on it and post again if I get totally stuck.

If it comes to it, I will use the SPI serial monitor code from that forum post (not I2C because I plan to use it later as part of this project). I don't have enough Arduinos right now, but I plan to get a few more soon, and this SPI serial echo method looks excellent for troubleshooting.

Thanks

EDIT: I should have been more ebullient in how neat Mr Gammon's SPI echo idea is. I really like the concept for this project and the future and thanks for the link.