Hi!
I have an application where I would like to establish 2-way communicate between two Arduino Nanos using I2C using "EasyTransfer". EasyTransfer comes with an I2C library, but when trying to establish 2-way communication using it failed I opened up the library and from what I can tell the library did not support this. I posted a message on the author's website here: Topic: EasyTransfer-I2C two-way communication « The Mind of Bill Porter
What lead me to believe that 2-way communication had not been worked out was the fact that the EasyTransferI2C::sendData contained code specific for sending data from a Master to a Slave and the receiveData contained code specific for receiving data from a Master to a Slave.
My solution was to modify this library and make i2c_address inputs to ::sendData() and ::receiveData() as optional. If the input is supplied, then it assumes Master -> Slave communication. If no input is supplied the code assumes that the Slave is responding to a Master request.
Attached is the library and the code. I think I'm close, but currently only Master -> Slave communication is working properly. My example code is an expansion of the original example code. Here is the gist of the processes (how it should work):
Master declares random "blinks" and "pause" variable
Master sends Slave this data
Slave receives the data, blinks and pauses the LED_INDICATOR based on the data
Master requests the Slave send data back to it
Slave send back the "blinks" and "pause" variables
Master receives the "blinks" and "pause" variables, proceeds to blink and pause.
Delay 5000, repeat.
I'm about to pull out the o-scope to see if I can troubleshoot this some more.
Thanks for your help! I apologize in advanced for my poor spelling in my comments.
From what I can tell from the O-Scope and adding some "Serial.print" debug lines in my library code, the only thing my slave is transmitting is the value 255, 6 times...
Hmmm..this is...weird. OK, I might just be missing...something but I think I'm zeroing in on the problem.
For a Master to queue to data to be sent to a Slave, the Master can call Wire.write() as many times as it likes. If going the other direction (Slave responding to a request for data from the master), you can seemingly only call Wire.write() once in response to the request.
If you change the line in the Slave code: Wire.write("hello ") with:
Wire.write('h')
Wire.write('e')
Wire.write('l')
Wire.write('l')
Wire.write('o')
Wire.write(' ')
The code will fail.
Solution? Probably rewrite the ::sendData() code to collect everything into an array of bytes and then transmit with one Wire.write() call. Working on that now.
I'll be submitting my code to Bill Porter for inclusion in his library. Hit me up with a PM if you need it. This makes 2-way communication over I2C using any data structure (<256 bytes) a breeze!
Nickerbocker:
If you change the line in the Slave code: Wire.write("hello ") with:
Wire.write('h')
Wire.write('e')
Wire.write('l')
Wire.write('l')
Wire.write('o')
Wire.write(' ')