Question about I2C: Master Reader/Slave Sender

Hey all,

I tried the following code : http://arduino.cc/en/Tutorial/MasterReader

and it was working nicely, but i want to modify further,
As you can see, in this example, the code will be able to see text "Hello " since it is already defined accurately on the following function:

  Wire.requestFrom(2, 6);    // request 6 bytes from slave device #2
}

And my question is, how should i write in order to have a new text?

For example, I wanted the output will be like this:

Hello
This is the new line

====================================
So, should i write another Wire.requestFrom for the text "this is the new line"?
I tried that way, but it doesn't work.
Is there anyway where I can send long message to the Master without limiting the byte size as formatted in Wire.requestFrom?

Thank you in advance

Stella

I tried that way, but it doesn't work.

Post your code please.

And read this:

Nick Gammon

This is the how i implement the function in the void loop of slave

 Wire.begin(2);
  Wire.onRequest(requestEvent);

  Wire.begin(3);
  Wire.onRequest(requestEvent2);
  //Wire.onRequest (CO2Read);

void requestEvent()
{
  Wire.write("Hello "); //respond with message of 6 bytes
                        //as expected by master
  Serial.println ("");
}

void requestEvent2()
{
  Wire.write("Welcome "); //respond with message of 6 bytes
                        //as expected by master
  Serial.println ("");
}

And this is how i implement on the master:

          Wire.requestFrom (2,15); //request 6 bytes from slave device #2
          while (Wire.available())
          {
          char c= Wire.read();
          Serial.print(c);
          }
          
          Wire.requestFrom (3,15); //request 6 bytes from slave device #2
          while (Wire.available())
          {
          char c= Wire.read();
          Serial.print(c);
          }

So in the end, it shows Welcome ÿÿÿÿÿÿÿ
I can understand that the ÿ appears because i suggested 15 bytes, which is too much for only text 'Welcome'.

But I wanted the result shows as
Hello
Welcome

Is there any way where i can directly show both texts without creating functions for each text?

void requestEvent2()
{
  Wire.write("Welcome "); //respond with message of 6 bytes
                        //as expected by master
  Serial.println ("");
}

Hardly 6 bytes, eh?

Don’t do Serial prints inside an ISR.

http://gammon.com.au/interrupts

  Wire.onRequest(requestEvent);

  Wire.begin(3);
  Wire.onRequest(requestEvent2);

You can only have one request event.