I2C Problem, Communicating Between Two Arduinos

Thank you for your response, it's finally working.

Thank you for your response, it's finally working.

Great. So, what was the problem? How did you correct it?

The problem was in using Wire.write() without buffer length in the slave code. Correct command in this case is Wire.write(msg,2);

Just wanted to confirm that this solved my problem.

I was dealing with a .onRequest event on my slave arduino and my response was 2x Wire.write(), and for some reason I was only receiving the first byte.

Adding .beginTransmission and .endTransmission around the .writes cause my arduino to hang.

Building and sending an array sans Transmission functions fixed my problem.

Many thanks :slight_smile:

For those who need to call write( ) multiple times in a slave response, I was able to modify twi_transmit( ) in twi.c to the following code and this works fine:

uint8_t twi_transmit(const uint8_t* data, uint8_t length)
{
 uint8_t i;
   // ensure data will fit into buffer
   if(TWI_BUFFER_LENGTH < twi_txBufferLength+length){
       return 1;
   }
   
   // ensure we are currently a slave transmitter
   if(TWI_STX != twi_state){
       return 2;
   }
   
   // set length and copy data into tx buffer
   for(i = 0; i < length; ++i){
       twi_txBuffer[i+twi_txBufferLength] = data[i];
   }
   twi_txBufferLength += length;
   
 return 0;
}