0
votes

I'm using an i2c serial bus for communication between two Arduino (Uno = Master, Due = Slave) and I'm currently experiencing problems while reading data received by the slave.

The master sends some data using Wire.write(command). The slave receives it and the handler function receiveEvent(int howMany) is called thanks the instruction Wire.onReceive(receiveEvent).

Here is the simplified code for the serial communication:

Master's Sketch

#include <Wire.h>

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

void loop() {
  Wire.beginTransmission(8);
  byte command[] = {2, 5, 3};
  Wire.write(command, 3);
  Wire.endTransmission();

  Serial.println("command sent...");
  delay(1000);
}

Slave's Sketch

#include <Wire.h>

int c = 0;

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

void loop() {
  delay(1000);
}

void receiveEvent(int howManyBytes){
  for(int iter=0; iter<howMany; iter++){
    c = Serial.read();
    Serial.print("c : ");
    Serial.println(c);
  }
}

Slave's Output

c : -1
c : -1
c : -1

It appears that three bytes are received but the data are not transmitted correctly. Any idea were there could be a mistake or a bug? Thanks!

1

1 Answers

1
votes

Since you expect the data from the Wire, I think your slave should receive the data via Wire.read() instead of Serial.read().