I2C can't read data Master ->Slave

Master code:

#include <Wire.h>
int dataSlave2[2];

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

}

int p = 11;
int k = 22;
 
void loop(){
  p = dataSlave2[0];
  k = dataSlave2[1];
Wire.beginTransmission(102);
Wire.write(dataSlave2[0]);
Wire.write(dataSlave2[1]);
Wire.endTransmission();
    delay(500);
}

Slave code:

#include <Wire.h>
int dataMaster[2];

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

void receiveData(){
   Wire.requestFrom(100,2);
   dataMaster[0] = Wire.read();
   dataMaster[1] = Wire.read();
 
}

void loop(){
  receiveData();
}

So theoreticaly I should get the value

array[0]: 11
and
array[1]: 22

but what I get is :

-1 , -1
-1 , -1
-1 , -1
-1 , -1
0 , 255
0 , 255
0 , 255
0 , 255
0 , 255
0 , 255
0 , 255
0 , 255
0 , 255

So… I’m not sure what’s the problem here.

I haven't looked at your code in detail but the first thing I see is that you have both units as slaves, 100 and 102. One has to be a master which in code would be:

wire.begin();

Try some of the examples in the wire library.

I haven't looked at your code in detail but the first thing I see is that you have both units as slaves, 100 and 102. One has to be a master which in code would be:

Yes I know and i tried that but how exactly I'm gonna send 2 values int/float using array and store them exactly as i send them from master to slave ?

Only the master can initiate a transaction. It can send a message or request some data. The slave can respond to the two types of messages using it's onReceive and onRequest events.

To allow a slave to send a message to the master, the master would periodically have to ask 'do you have anything to send' via a single coded byte using the onReceive event. That byte could have many meanings such as, give me the temperature or the humidity or any number of pieces of data. The master would then get the slaves answer via the onRequest event. A single byte is sufficient, 0 means nothing to send, and 1 through 32 is the quantity to send. If the value is not 0 then the master would initiate another onRequest event and receive the correct amount of data. The slave would already know what to send from it's answer to the first question. The links below show you how to make it work both ways.

Arrays are sent/read using a byte pointer to the beginning of the array and the size of the array.

MasterReader

MasterWriter

Arrays are sent/read using a byte pointer to the beginning of the array and the size of the array.

Master Code

#include <Wire.h>

const byte SlaveDeviceId = 1; //use your slave address here
int sendInts[2];

void setup()
{
  // Start I²C bus as master
  Wire.begin();
}

void loop()
{
  sendInts[0] = 456;
  sendInts[1] = 567;
  Wire.beginTransmission(SlaveDeviceId);
  Wire.write((uint8_t*)&sendInts, sizeof(sendInts));
  Wire.endTransmission();

  delay(1000);
}

Slave Code Note how there is no serial printing within the receive event interrupt handler

#include <Wire.h>

const byte SlaveDeviceId = 1; //use your slave ID here
volatile int receiveInts[2];
volatile boolean newReceiveEvent = false;
volatile boolean errorEvent = false;

void setup()
{
  // Start I²C bus as a slave
  Wire.begin(SlaveDeviceId);
  Wire.onReceive(receiveEvent);//interrupt handler for incoming message

  Serial.begin(9600);
  Serial.println("starting....");
}

void loop()
{
  if (newReceiveEvent == true)
  {
    for (byte j = 0; j < 2; j++)
    {
      Serial.println(receiveInts[j]);
    }
    newReceiveEvent = false;
  }

  if (errorEvent == true)
  {
    Serial.println("I2C Reveive Error");
    errorEvent = false;
  }
}

// aCount is the number of bytes received.
void receiveEvent(int aCount)
{
  if (aCount == 4)
  {
    Wire.readBytes((uint8_t*)&receiveInts, 4); //buffer read with direct cast
    newReceiveEvent = true;
  }
  else
  {
    errorEvent = true;
  }
}

Can you explain me this line please:

Wire.write((uint8_t*)&sendInts, sizeof(sendInts));

Can't i skip the "SlaveDeviceId" and use a direct number? as I know i can use from 0 to 127.

About the receiver/ Slave can't i make the "receiveEvent/loop" more compact instead of using all that lines of code? It's not about that i can't undersant or learn but I will give it a try for sure.

So the

aCount is the number of bytes received.

We send int values which we got the int as 2bytes -32,768 to 32,767, we receive 4 bytes and store the 2 1st bytes as a value in the array id:0 and the next to in the array id:1. Am I right? Is there a possibility that they may not be received in the right order or missing bytes?

So that gonna work for the float..etc as well, right?

Can u explain me this line as well pls

Wire.readBytes((uint8_t*)&receiveInts, 4);

read byte " ,4 I've seen uint8_t..etc in arduino links but couldn't find it right now.

cattledog

You code got errors

Slave invalid conversion from 'uint8_t*' to 'char*'

cattledog

You code got errors

Slave invalid conversion from 'uint8_t*' to 'char*'

Both Master and Slave codes I posted compile correctly and send and receive the two integers. It's running on my desk as I type this.

Please post the code which produced the error and identify the line producing the error.

Can't i skip the "SlaveDeviceId" and use a direct number? as I know i can use from 0 to 127.

Yes, you can do that?

we receive 4 bytes and store the 2 1st bytes as a value in the array id:0 and the next to in the array id:1. Am I right? Is there a possibility that they may not be received in the right order or missing bytes?

Correct. The bytes will be received in the correct order. The possibility of missing bytes is why there is an error test on bytes received.

So that gonna work for the float..etc as well, right?

Yes this approach with work for floats, or if you use a struct it can be mixed data types.

Can u explain me this line as well pls Quote Wire.readBytes((uint8_t*)&receiveInts, 4);

It tells the wire library to read four bytes into the memory addresses holding array receiveInts.

cattledog

Thanks for your time helping me and for your code, I done a small research about variables on internet and found this site by accident

http://www.bajdi.com/i2c-communication-between-2-arduinos/

For me it's a nice and easy / simple example to transfer integers / floats ..etc thru I2C I took this example and worked on it, it's working without problems.