Sending variables using I2C and Java.

Dear forum members!

I am reading all inputs and outputs from one Arduino due and trying to send more than 1 variable back to Raspberry Pi 3. I am using I2C
because I am going to have 8 Arduino boards connected to one Pi 3. In the beginning, I tested with a simple code on one Arduino and it's working but I can't send the pins who have "0" back to my Raspberry. With this code, I can send just the output "x" or the input "y". I don't know if I need to use an array or struct so I'll be grateful if you help me to fix that. If pin 13 is connected to pin 3 with a cable and pin 45 is connected to pin 39 with another cable, on Arduinos Seriell monitor looks like:

There is a 0 on pin 13
There is a 0 on pin 3
There is a 0 on pin 45
There is a 0 on pin 39

There is a 0 on pin 13
There is a 0 on pin 3
There is a 0 on pin 45
There is a 0 on pin 39

There is a 0 on pin 13
There is a 0 on pin 3
There is a 0 on pin 45
There is a 0 on pin 39

But on The Raspberry, I am receiving just 3.
Here is my code:

#include <Wire.h>
#define SLAVE_ADDRESS 0x5 // Slave board with adress 0x5
const byte PinRangeLowestPin = 2;
const byte PinRangeHighestPin = 53;
byte pin;
int x;
int y;

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) {
        x = outPin;
        y = pin;
        Serial.print("There is a 0 on pin ");
        Serial.println(pin);
      }
    }

    pinMode(outPin, INPUT_PULLUP);
  }

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

// callback for sending data
void sendData() {
  Wire.write(y);
  Wire.write(x);
}

Thanks in advance!

Global variables access in interrupt context need to be declared volatile otherwise the compiler might optimize them away or use old values.

Hint: sendData() is called in interrupt context.

Thanks for your reply!