0

In the master write/ slave read example for the wire library, there is an integer declared howMany that is never used.

 void setup() {
   Wire.begin(8);                // join i2c bus with address #8
   Wire.onReceive(receiveEvent); // register event
   Serial.begin(9600);           // start serial for output
}

void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
   char c = Wire.read(); // receive byte as a character
   Serial.print(c);         // print the character
}
 int x = Wire.read();    // receive byte as an integer
 Serial.println(x); 
}

As the function does not work without it, i would like to know what is its exact purpose.I would imagine that is something regarding the buffer size.

  • The description of the wire commands are extremly poor. For me there is also a lot of guessing involved. Without example codes from foren for me it is not possible to generate a running code. For that reason the question for the sense of how many is very valid. – User111 Jun 24 '16 at 15:29
2

It tells you how many bytes have been received, and consequently how many you can read with Wire.read().

Before you have read anything with Wire.read() Wire.available() will return the same value. As you read from the buffer Wire.available() will decrease, but howMany will stay the same.

1

The reference page https://www.arduino.cc/en/Reference/WireOnReceive states:

Parameters

handler: the function to be called when the slave receives data; this should take a single int parameter (the number of bytes read from the master) and return nothing, e.g.: void myHandler(int numBytes)

I suggest you look at the reference pages for the functions in future before asking what they do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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