Uno to Uno serial communication...

Hi,

I haven’t yet attempted to connect two boards together to communicate with each other, but this is what I essentially want to accomplish:

I would like to send/transfer the value of eight boolean variables on the ‘sending’ uno to eight boolean variables on the ‘receiving’ uno. These need to be updated constantly and I cannot have delays since the loop() function constantly polls for button presses. Does it make sense to transfer bit by bit, one variable at a time? Or do I shift all the date into one byte, send the byte, then shift each bit back out?

I’ve never attempted this before and the tutorials I see seem to all show how to send character and string data. I’ve also never used the << or >> operands for bit shifting.

Some tutorials show serial setup with i/o pins, some speak of I2C.

More simply put, what if I have the same boolean and integer variables on both the sending and receiving uno’s and I want the sending uno to constantly update the ‘receiving’ uno such that variables on each board always have the same value. This, being done with almost no delay in loop() so it wont miss any button presses.

8 bits? Quite easy. SPI/I2C/Serial - all work in byte mode - which is 8 bits.

It doesn't matter what you choose. If you want it really fast, choose SPI. If you want it really easy, choose Serial.

Definately have a look at bit shifting, you'll need it.

Write the byte at the one end and read the byte on the other end.

Serial input basics: Serial Input Basics - Programming Questions - Arduino Forum

well the "More simply put, what if I have the same boolean and integer variables on both the sending and receiving uno's and I want the sending uno to constantly update the 'receiving' uno such that variables on each board always have the same value. This, being done with almost no delay in loop() so it wont miss any button presses.

if you worry about missing a button press compared to electronic transmission of 1 byte (the value of eight boolean variables can be stuffed into 1 byte, each bit representing true or false with 1 or 0) then stop worrying...

Even with Software Serial at 9600 bauds your 1 byte will take 1ms to be sent from one arduino to the other. This seems an eternity in Arduino's time but compared to a button press, that's still super fast, your will have received at least 15 updates while your button is still bouncing from the operator press (if you code correctly the debounce)

of course if you don't use the hardware serial ports on your arduinos (i.e. you de-connected them from the USB and console) then you can use the hardware serial and go at 115200 without any issue (or even faster but it gets less reliable). Last but not least, with a very little more work you can then go use SPI and be ultra fast.

EDIT: coming late to the party, other answered as I was typing and got distracted .. :slight_smile:

Use bitWrite(x, n, b) to set the values at the sender and bitRead(x, n) to determine the values at the receiver. You can then send/receive the byte by any of the methods previously mentioned.

Thank you for the replies!

  1. Yes.

  2. No. Don't use delay().

  3. 32 bytes is the maximum. This is a limitation of the Arduino library. You can edit the library but normally there's no need. Either you are sending short things or you are sending something so long that no feasible buffer can hold it all at once.

  4. There may be some libraries, like Nick Gammon's sendAnything() but it's usually best to do this yourself.