Solved I2C freezes arduino

Hi,
I am making a quadcopter with an Arduino Mega 1280 and the sensors ITG3200, ADXL345, LSM303 and BMP085. All of these use I2C protocol.
The problems came when I turn on the brushless motors and my Arduino was freeze. In this post I found the solution for this:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283887406

But some days ago, when I tried to use all of my sensors, my arduino restart. Researching this problem, I could see that my free SRAM was reducing and after some days debugging all of my code I could see that the problem came from the solution previous.

Every time that the timeout is true, the i2c is restarted. In the twi_init() we allocate the buffers again and the SRAM is reduced every time.
I attach the simple modification which solve this situation.

I hope that it will help you and sorry for my english :slight_smile:

twi.c (14.9 KB)

twi.h (1.63 KB)

That library should be declaring the buffers statically, not allocating them dynamically - twi_init() then just zeroes out anything appropriate.

The underlying problem is that you have crosstalk from the motor circuitry into the TWI bus which causes the hang-ups in the first place - that's worth fixing properly if you want reliable operation. Keep high current and logic signals well separated, shielded, and there should be a single point where GND meets between the two systems.