Using data from Wire library in other functions

Something like below might do the job.

byte x;
bool I2Cready;

void setup()
{
  ...
  ...
}

void loop()
{
  ...
  ...
  if(I2Cready == true)
  {
    Serial.print(char(x)); // print char
    I2Cready = false;
  }
}

void receiveEvent()
{
  x = Wire.read();    // receive byte
  I2Cready = true;
}

Don't use single letter global variables. Instead of x, use e.g. receivedI2Cdata.

I'm not sure if the variables in receiveEvent need to be volatile.

Not tested

1 Like