0

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 Answer 1

1

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

1
  • You are right, it solved the problem... I can't believe I didn't see this!! Thanks a lot!! Dec 7, 2015 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.