synchronous master-slave communication

bigfun:
i did it because i'd often get zero instead of the byte i'd requested. i have no idea why -- it was being flaky. i need to investigate further. the other i2c devices on the bus all seem to work fine; it's just the slave that occasionally throws out glitch zeroes in response to queries.

Glad my examples helped. As for the zeros, do you check that there is data available? (if (Wire.available)?). If it passes that test, the data should be good. If not, the slave may have taken too long to respond. I haven't found a quoted maximum time for the response but I assume you are supposed to do it almost immediately. Also it might conceivably help if the master did a "repeated start" for the response rather than "end/start" but I don't know that the Wire library supports that right now.

As for the timing, it isn't clear exactly how long your slave takes to respond, but if it has to do something (like an analog measurement) that might take too long. You could take a leaf from the 1-wire temperature sensors, and have two messages.

Eg. Message 1: take a reading. (pause 50ms). Message 2: give me your most recent reading (and then request a response). That way you introduce a delay to let the slave actually find the data. But this is still synchronous because you do these things in sequence at the master end.