Long data through I2c

Hi at all,

for a personal project I'm trying to move a stepper motor (with a dedicated stepper board) from an atmega 328p, with I2c bus another arduino will pass at the atmega all the parameters of the stepper.

I need to pass through I2c the speed of the stepper (0-4000 value), the acceleration (0-4000 value) and the targhet_position (-10000 - +1000 value).

The only method that I found to do that is to separate my numbers and build a bit data array and rebuild my number with a dedicated code in the 328.

So my question is: is there some more efficent ways to move those data? I may rest in I2c or better move to another transmission protocol?

The targhet for me is to say on the master: (pseudo code)

Stepper1(328_number, pin, pin); /* With Easy driver I need only Step pin and Dir pin, and stepper attach to atmega 328_number */


And a part of code will automatically redirect the information on the right atmega to the right stepper, so first define the engine not attach directly but on the right atmega.

I hope to be as clear as possible.

Thanks in advance! Riccardo

So my question is: is there some more efficent ways to move those data?

Yes. An int is two bytes. The highByte() and lowByte() functions (macros, actually) will extract the two bytes. Store them in a byte array, and send all 6 bytes at once.

Or, use a union to create a 3 element int array and a 6 element byte array occupying the same memory. Store the three ints in the int array and send the byte array.

Re-constructing the int from the byte is a simple matter of multiplying the high order byte by 256 and adding the low order byte. Or, use the same union and read into the byte array and grab the data from the int array.

The Wire library can go up to 32 byte per I2C transmission. Those 32 bytes can be anything, like 16 integers, or 8 float numbers, or a combination of them. I use a 'struct' as a data packet, and just fill in the struct what needs to be transferred.

Thanks, in these days I'll try some and I'll write here news soon.