I m using I2C between two arduinos. For the slave receiver I have a receiveEvent function for receiving data. eg void receiveEvent(int howmany){}
I d like to know when data is received. I was wondering is it possible to make the receveiveEvent function boolean? eg
boolean receiveEvent()
{
while(1 < Wire.available())
{
return true;
}
else
{
return false;
}
}
So basically my implementation is the standard void receiveEvent() function that contains a boolean variable to let me know when data has arrived. However this means the boolean variable has to be global in order to use it in other functions. I d like a more elegant solution to reduce global variables or is this possible?
You can just use the Wire.available() wherever you want to check if data is available. If you need it as a boolean, try:
if (Wire.available()>0) receiveEvent = true;
else receiveEvent = false;
receiveEvent can be a local variable in the function.
If you specifically want a receiveEvent() function try:
boolean receiveEvent()
{
if(Wire.available()>0) return true;
else return false;
}
You don't need a global variable, since you call the receiveEvent function when you check for data. The function returns your flag:
if (receiveEvent()) {
//RUN THIS CODE WHEN DATA IS AVAILABLE
}
Hope it helps 
romarshmallow, we can't give a good answer if we don't know what receiveEvent() is. http://snippets-r-us.com/
SandraOos, the function receiveEvent() is probably the onReceive interrupt handler. Wire - Arduino Reference
The interrupt handler can't return something.
I know you have seen this example : http://www.arduino.cc/en/Tutorial/MasterWriter
I'm sorry to say, but that is a terrible example. The number of received bytes is in the variable 'howMany', there is even no need to call Wire.available().
To pass information from the interrupt handler to the code in the loop(), use a flag, or a variable, and make it volatile.
volatile flagGotSomething = false;
...
void loop()
{
if( flagGotSomething)
{
flagGotSomething = false; // reset flag.
Serial.println("Got Something");
}
}
void receiveEvent(int howMany)
{
// howMany is the number of received bytes.
flagGotSomething = true;
}
Why make a function to return T/F just to run a function that returns the same T/F?
Yes, when i said receiveEvent i was referring to the onReceive interrupt handler. To Peter_n, I have something similar to the flagGotSomething. I was hoping to reduce the presence of the if statement just for making my code more neat. I saw that the interrupt handler doesnt have a return type so i guess there doesnt seem to be a more simplified way of doing this. Oh well.
A volatile flag is super neat 
The interrupt handler is called from the Wire library. If there was a return value, that value would end up in the Wire library. That would be not useful for the sketch.