Question about the transferring an array of integers via I2C bus

I am currently stuck with a problem with the I2C bus when dealing with integers and arrays that I'm hoping someone has a solution for.

I have and array along this line: transferArray[5] = {25,10,7,2080,31240};

In the environment I have the values vary regularly as data is collected and manipulated before going into the array.

I need to both read the array from the slave to the master and send from the master to the slave in the same format.

I can see that sending bytes is straight forward and also sending a single integer is straight forward, but I'm struggling to get my head around the sending the array of integers making sure you have the data from the first cell of the array lines up at both ends.

To complicate this a little also is the fact that there are several slave boards so timing of the getting of the data will be important (Perhaps beginTransmission and endTransmission are used to ensure the integrity of data?

The only constant references I have are the 5 integers and the slave addresses which I cycle through checking to see if there is any data to be read back to the master.

Thanks in advance

you could cast your int array address to byte *, e.g.

void send()
{
  int transferArray[5] = {25,10,7,2080,31240};
  i2cTransmit((byte *) transferArray, sizeof(transferArray));
}

// I2C transmit data
int i2cTransmit(byte *data, int size)
{
  Wire.beginTransmission(0x0B); // transmit to device 
  //Serial.print("(I2C Tx ");
  for(int i=0;i<size;i++)
    {
  //  Serial.print(data[i], HEX);  Serial.write(" ");
    Wire.write(data[i]);              // sends one byte
    }
 // Serial.print(") ");
  Wire.endTransmission();    // stop transmitting
}

1. Let us take the following hardware I2C Bus setup as an example to transfer the data of this array: int transferArray[5] = {25,10,7,2080,31240}; from UNO (the Master) to NANO (Slave).
i2c-ms.png

Master Code:

#include<Wire.h>
int transferArray[5] = {25, 10, 7, 2048, 31240};
//int transferArray[5] = {0x0019, 0x000A, 0x0007, 0x0800, 0x7A08};
void setup()
{
  Serial.begin(9600);
  Wire.begin();
 // Serial.print(sizeof(transferArray));

  Wire.beginTransmission(0x08);
  for (int i = 0; i <(sizeof(transferArray))/2; i ++ )
  {
    Wire.write(highByte(transferArray[i])); //I2C is byte-oriened Bus
    Wire.write(lowByte(transferArray[i]));
  }
  Wire.endTransmission();

}

void loop()
{

}

Slave Codes

#include<Wire.h>
int j = 0;
byte x;
int y;
byte recArray[10];
int receivedARry[5];

void setup()
{
  Serial.begin(9600);
  Wire.begin(0x08);
  Wire.onReceive(receiveEvent);
}

void loop()
{

}

void receiveEvent(int howMany) //howMant is always equal to bytes receuved
{
  for (int i = 0; i < howMany/2; i++)
  {
    x = Wire.read();
    j++;
    if (j == 1)
    {
      y = (x<<8)|Wire.read();
      Serial.print(y, DEC);
      Serial.print(' ');
      j=0;
    }
  }
}

Serial Monitor of Slave
scr-14.png

2. Program-A is to Receive an int type array (int transferArray[5] = {125, 210, 7123, 12048, 3240}:wink: from Slave.

Master Codes:

#include<Wire.h>
int j = 0;
byte x;
int y;

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  Wire.requestFrom(0x08, 10);  //to receive 10-byte
  for (int i = 0; i < 10/2; i++)
  {
    x = Wire.read();
    j++;
    if (j == 1)
    {
      y = (x << 8) | Wire.read();
      Serial.print(y, DEC);
      Serial.print(' ');
      j = 0;
    }
  }
  Serial.println();
}

void loop()
{

}

Slave Codes:

#include<Wire.h>
int transferArray[5] = {125, 210, 7123, 12048, 3240};
//int transferArray[5] = {0x0019, 0x000A, 0x0007, 0x0800, 0x7A08};
bool flag1 = LOW;


void setup()
{
  Serial.begin(9600);
  Wire.begin(0x08);
  Wire.onRequest(sendEvent);
}

void loop()
{
  
}

void sendEvent(int howMany)
{
   for (int i = 0; i < (sizeof(transferArray)) / 2; i ++ )
    {
      Wire.write(highByte(transferArray[i])); //I2C is byte-oriened Bus
      Wire.write(lowByte(transferArray[i]));
    }
}

Master's Serial Monitor
scr-15.png

3. Program-B is to Receive an int type array (int transferArray[5] = {125, 210, 7123, 12048, 3240}:wink: from Slave.

... to be continued

i2c-ms.png

scr-14.png

scr-15.png

1 Like

Thank you so much for your replies it is really appreciated. I shall study and see how you have come at the result that I was after and then implement the result of this in my project.