Why can't count in the void loop()?

Hello all!
I want to send an array to Raspberry. If I have my for Statement in the void sendData() my output is:
0
1
2
3
4
But when I have the statement in the void loop() then I am getting:
80
230
20
66
105
My question is how to fix a counting in the void loop() and returning “f” to the void sendData()?
Thanks in advance!

Here is my code:

#include <Wire.h>
#define SLAVE_ADDRESS 0x5
#define SIZE 5
int a[SIZE];
int f = 0;

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

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

void loop() {

  //    for (int b=0; b<SIZE; b++){
  //    f=a[b];
  //    a[b]++;
  // }
}

// callback for sending data
void sendData() {
  for (int b = 0; b < SIZE; b++) {
    f = a[b];
    a[b]++;
    Wire.write(f);
  }
}

If you want to send 1-2-3-4, why do you have an array and why are you changing the values in the array?

Your question isn't very clear. Perhaps if you give us some background on what you are counting or what makes the values change?

sendData is called only if an I2C request for data is received while the same code in loop() is called constantly as long as the Arduino is powered on.

You current code is not correct as you have to declare the array "a" volatile. sendData is called in interrupt context, the compiler cannot find out that it is even called and may therefor make optimization decisions that doesn't result in what you expect.

Here is my Java code for Rasberry (Master code):

package i2c_test2;

import java.io.IOException;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;


public class I2C_Test2 {

	public static void main(String[] args) throws Exception {
		try {
			 printSystemInformation();
	            System.out.println("Creating I2C bus");
	            I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
	            
	            System.out.println("Create I2C device nr 1");
	            I2CDevice device = bus.getDevice(0x5);
	            
	          int i=0;
			  while (i<5) {
				    int dataRead = device.read();
	                System.out.println("Read " + dataRead ); 
	                i++;
	              }
			 } catch (IOException ex) {
			  ex.printStackTrace();
			 } catch (InterruptedException ex) {
			  ex.printStackTrace();
			 }
	}

public static void printSystemInformation() throws IOException, InterruptedException {
	//System.out.println("CPU Temperature   :  " + SystemInfo.getCpuTemperature());
   
    System.out.println("----------------------------------------------------");
    System.out.println("CLOCK INFO");
    System.out.println("----------------------------------------------------");
    
}
}

Here is my previous code for the Master which is generating random numbers constantly. I changed the code because I want to read pins constantly without writing information to the slave. I want just to constantly read numbers from Arduino.

Master with Random:

package i2c_test2;

import java.io.IOException;
import java.util.Random;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;


public class I2C_Test2 {

	public static void main(String[] args) throws Exception {
		try {
			 printSystemInformation();
	            System.out.println("Creating I2C bus");
	            I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
	            
	            System.out.println("create I2C device nr 1");
	            I2CDevice device = bus.getDevice(0x5);
	            
	            byte[] writeData = new byte[1];
	            long waitTimeRead = 5000;
	              
			    while (true) {
				   //negative values don't work
	                new Random().nextBytes(writeData);
	               System.out.println("Writing " + writeData[0] + " via I2C");
				  System.out.println("Skickar in 1 via I2C ");
	                
				   device.write(writeData[0]);
	                
	                System.out.println("Read via I2C from device 1");
	                int dataRead = device.read();
	                System.out.println("Read " + dataRead );
	                System.out.println("weit 5 sek");
	                Thread.sleep(waitTimeRead);
	                
	           }
			 } catch (IOException ex) {
				  ex.printStackTrace();
				 } catch (InterruptedException ex) {
				  ex.printStackTrace();
				  }
				 }
	

public static void printSystemInformation() throws IOException, InterruptedException {
	//System.out.println("CPU Temperature   :  " + SystemInfo.getCpuTemperature());
   
    System.out.println("----------------------------------------------------");
    System.out.println("CLOCK INFO");
    System.out.println("----------------------------------------------------");
    
}
}

And this is the Slave code with Random:

#include <Wire.h>
#define SLAVE_ADDRESS 0x5
int number = 0;

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

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

void loop() {
  delay(200);
}
// callback for received data
void receiveData(int byteCount) {
  while (Wire.available()) {
    number = Wire.read();
  }
}
// callback for sending data
void sendData() {
  Wire.write(number);
}

With those codes, I am writing to and reading from Arduino Random numbers. But I want just to read from Arduino.

But I want just to read from Arduino.

Use a different mechanism, then. That is not the way I2C works.

Okay, I will try. Thanks för your reply!

I want just to constantly read numbers from Arduino.

Why did you choose I2C for this?

Might be a bad question but why did you choose Java for the Raspi side? I'm asking because Java is abstracting a bit to much from the low level interface I2C and what worse, the documentation for the library does not tell you what kind of abstraction it does. The sentence "This method reads one byte from the i2c device." does not tell you if that is done in a complete I2C request or if that is coupled to the last call or anything like that. But for I2C that is important. The abstraction resembles the method to read from UART style interface but I2C is very different.

What kind of data do you plan to read from the Arduino?