sending 2 Arrays via I2C to Pi with Java

Hi everyone!
Did someone know how to send 2 arrays to Raspberry with Java. I tried with this code but I am receiving just the first array 2 times.I can not send more than 32 bytes at one time. Thanks in advance!
Slave:

#include <Wire.h>
#define SLAVE_ADDRESS 0x5 // Slave board with adress 0x5

void setup() {
  // initialize i2c as slave
  Wire.begin(SLAVE_ADDRESS);

  // define callbacks for i2c communication
  Wire.onRequest(sendData);
  Serial.begin(9600);
}

void loop() {

}
void sendData() {
  byte buf1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
  17,18,19,20,21,22,23,24,25,26};
  byte buf2[] ={27,28,29,30,31,32,33,34,35,36,37,38,39,
  40,41,42,43,44,45,46,47,48,49,50,51,52};

  Wire.write (buf1, sizeof (buf1));
  Wire.write (buf2, sizeof (buf2));
}

I guess you have to restructure sendData() so the first time it is called, it responds with buf1. The second time it is called it should respond with buf2. And so on.

Don't you need Wire.beginTransmission() and Wire.endTransmission() calls before and after the Wire.write() calls?

Hi! I cant restructure sendData because its an imported file from Java libraries and I don't know how to find it, therefore, I am trying to fix that in Arduino code. I tried to increase 32 bytes to 64 like in this example: Increase 32 byte I2C buffer size in Wire library - Networking, Protocols, and Devices - Arduino Forum but it did not work.
About Wire.beginTransmission() I tried with this code but still sending the first 32 bytes:

void sendData() {
  byte buf1[]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53};

 Wire.beginTransmission();
  for(int x = 0; x < 52; x++) {
    Wire.write(buf1[x]);
  }
  Wire.endTransmission();
}

The beginTransmission() function sets up a 32 byte buffer to contain the data to be sent. You can NOT store 52 bytes in a 32 byte array. When you have crapped all over memory you don't own doing that, you can NOT rely on ANYTHING the code does after that.

The endTransmission() function is what causes the contents of the buffer to actually be sent.

Hi! I tested with this code and it did not work

void loop() {
 Wire.beginTransmission (SLAVE_ADDRESS);
 byte buf1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
 Wire.write (buf1, sizeof (buf1));
 Wire.endTransmission ();
 delay(5000);
}

Hi! I tested with this code and it did not work

It did something. You must tell us what it actually did. You must tell us what you expected it to do. You must explain why you think "it did not work".

I am reading from my "Arduino Due" 50 pins and I want to send bytes threw I2C to raspberry. For coding the raspberrian I am using Java. I have taken an example from this page:I2C Arduino and Raspberry pi using pi4j | Dev Stuff
and made it just to receive data from Arduino and it works. But I can't send more than 32 bytes at one time and I want to send 50. This code works:

void sendData() {
  byte buf1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
  17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};

  Wire.write (buf1, sizeof (buf1));

}

I am reading from my "Arduino Due" 50 pins

So, 50 bits, or 7 bytes. Why do you think you need to send 50 bytes?

I want to send trow I2C an array with a length 50 bytes. In Arduino due I have 53 pins I am not using "pin 0-RX" , "pin 1-TX " and "20-SCL", "21-SDA" because of I2C connection. So I have 50 pins and going to read if there is "0" if a cable is connected between any of them. If I have 50 cables connected to all pins I need to send information that in pin 1....50 is zeroes. It can be there is 0 on pin 2,3,23,24 or all of them.

With this code I am riding 50 pins and can return just 32, so, therefore, I split the array into 2 pieces and wanted to send them to raspberryan.

#include <Wire.h>
#define SLAVE_ADDRESS 0x5 // Slave board with adress 0x5
#define SIZE 50

byte buf[SIZE];
const byte PinRangeLowestPin = 2;
const byte PinRangeHighestPin = 53;


void setup() {
  // initialize i2c as slave
  Wire.begin(SLAVE_ADDRESS);

  
  // define callbacks for i2c communication
  Wire.onRequest(sendData);

  Serial.begin(9600);
 
  for (byte pin = PinRangeLowestPin; pin <= PinRangeHighestPin; pin++) {
    if (pin == SDA || pin == SCL) continue;
    pinMode(pin, INPUT_PULLUP);
  }
  
}

void loop() {
  for (byte outPin = PinRangeLowestPin; outPin <= PinRangeHighestPin; outPin++) {
    if (outPin == SDA || outPin == SCL) continue;
    pinMode(outPin, OUTPUT);
    digitalWrite(outPin, LOW);

    for (pin = PinRangeLowestPin; pin <= PinRangeHighestPin; pin++) {
      if (pin == outPin || pin == SDA || pin == SCL) continue;
      //Serial.print(outPin); Serial.print("->"); Serial.println(pin); // debug
      if (digitalRead(pin) == LOW) {
         //Serial.print("There is a 0 on pin ");
         //Serial.println(pin);
         buf[pin]=pin;
      }
    }

    pinMode(outPin, INPUT_PULLUP);
  }

  //Serial.println("========================");
  // delay(5000);
}

// callback for sending data
void sendData() {
Wire.write (buf, sizeof (buf));
}

So I have 50 pins and going to read if there is "0" if a cable is connected between any of them. If I have 50 cables connected to all pins I need to send information that in pin 1....50 is zeroes. It can be there is 0 on pin 2,3,23,24 or all of them.

So, you want to read a pin state, which will give you 0 or 1 - 1 bit of data. You then want to store than 1 bit of data in an 8 bit array element, wasting 7/8 of the space.

And, then you want to send 400 bits of data, 350 of which are useless, even though you KNOW that you can't do that.

I'll ask again. Why do you need to send 50 bytes of data to transfer 50 bits of information?

Ok. Could you please tell me how to fix that? Should I have integer instead byte?

Tonyboy:
Ok. Could you please tell me how to fix that? Should I have integer instead byte?

Which is bigger - an int or a byte? Look at the range of values that each can hold if you need a hint.

The documentation for the bitWrite() macro would make for interesting reading.

You should investigate "bit fields" in C++. If for no other reason that if you can't use them for this, that you might later on.

Byte and char are 8 bits.