i2C question on Wire.onRequest()

Hello. From my knowledge Wire.onRequest() allows you to send send data to Master on request. However what if I want to request 2 different types of data?
For example,

Master will request from slave A an 'OK' on start up. After that
Master will request again from slave A an input data.

void startupTest() 
{
  Wire.requestFrom(8, 4);
  while (Wire.available()) 
  { 
    char startA = Wire.read(); // receive a byte as character
    Serial.print(startA);         // print the character    
  }
}

void requestA() 
{
  Wire.requestFrom(8, 6);

  while(Wire.available())
  {
    float A = Wire.read();
    float displayA = A / 255;
    Serial.print(displayA);
  }
}

void loop() {

  startupTest();
  
  while(1){
    requestA();
 }
}

Was wondering if the above code would work?
Therefore my question is, how would I let the slave know the difference between the first request and any other request?

No, because it will not compile without code. Please post whole code only! http://snippets-r-us.com

But assuming a setup with a Serial.begin() and a Wire.begin() (and that would also be the place for startupTest() instead of the ugly while(1)), then kind of. But how does the slave know the difference between the first request and any other request?

And a float from a single byte is not going to work :wink: A float is 4 bytes :slight_smile:

@septillion Yes definitely there is the "Serial.begin() and a Wire.begin()" , that is my bad on that for not posting it.

septillion:
But how does the slave know the difference between the first request and any other request?
And a float from a single byte is not going to work :wink: A float is 4 bytes :slight_smile:

Now that is a better phrasing of my question, how would I let the slave know the difference between the first request and any other request?
Also what does it mean by "And a float from a single byte is not going to work :wink: A float is 4 bytes :)" ?

bryanyon:
Master will request from slave A an 'OK' on start up. After that
Master will request again from slave A an input data.

You don't "request an OK". You request x number of bytes.

See Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino.

You normally send something first to the slave (to indicate what you want back) and then read that thing back. Something like this:

 Wire.beginTransmission (SLAVE_ADDRESS);
  Wire.write (cmd);
  Wire.endTransmission ();
  
  Wire.requestFrom (SLAVE_ADDRESS, responseSize);

In this example we "send" cmd, and request back a response of responseSize (which we then have to read, byte by byte).

Thank you! With the link you have provided along with the explanation I now understand how I should go about doing this.

Had a look at floats? They are 4 bytes big. And Wire.write() only takes 1 bytes...