Wire - Master Receiver/Slave Sender behavior

I’m sorry for being such a noob.
I still don’t get the point in this:
These codes are from the examples provided from the arduino package.
Master Receiver:

#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(5, 6);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  {
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave sender:

#include <Wire.h>

void setup()
{
  Wire.begin(5);                // join i2c bus with address #2
  Wire.onRequest(requestEvent); // register event
}

void loop()
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
  Wire.write("hello "); // respond with message of 6 bytes
                       // as expected by master
}

WHen I copy and paste the snippets into my project it all works fine. Now, if I alter the sent data from string to single char like this:

void requestEvent()
{
  
 Wire.write("h"); 
 Wire.write("e"); 
 Wire.write("l"); 
 Wire.write("l"); 
 Wire.write("o"); 
 Wire.write(" "); 
  
}

it stops working.
Can someone explain to me what’s happening here? Originally I am trying to send a long in a union bytewise, that doesn’t work whenever I try to transmit more than one byte at once using different lines (like, in a loop) it sends empty bytes.

If that matters I’m using Nanos on both ends.

Hi spacko

Problem may be multiple writes in event handler. There is a solution in this thread, using byte array and a single write:

http://forum.arduino.cc/index.php/topic,109335.0.html

Regards

Ray

Thank you very much Ray,
that has put me on the right track. I actually had to transfer a “long” and couldn’t just transfer bytes in a for-loop.
Solution looks as follows for Master:

union sendsum {
  long usumme;
  byte b[4];
};
 union sendsum usum;
.
.
.
  if (Wire.requestFrom (5, sizeof usum) == sizeof usum){
       for (int i = 0; i < sizeof usum; i++)
         usum.b[i] = Wire.read ();
      }

and for Slave:

long sum;
byte data[4];
.
.
.
  data[0] = sum;
  data[1] = sum>>8;
  data[2] = sum>>16;
  data[3] = sum>>24;
.
.
.
void requestEvent(){
   
  Wire.write(data,4);
 
}

It can be that simple…
Thanks again, I really appreciate it!

hello ' i need code master receiver /slave writer char or int just code work