Can’t seem to find the answer Goggling. Example code is often not a good source as only the happy path is coded properly.
When Arduino is acting as a master and requesting a single byte read, your code would look like:
// Request the byte
Wire.requestFrom(add, 1);
// Determine if we have the byte */
n = Wire.available();
Question: Is the requestFrom blocking? By the time you return from the call has the peripheral attempted to clock in all the bytes and ACKS? Or does one need to spin in a wait loop for some timeout period waiting for the bytes to arrive?
The reason I ask is that under a stress test, I will occasionally not get the needed single byte I requested. Wire.available will return a zero. The failure rate changes from 10% of the time to close to 0% based on what code I have loaded. I have not been able to determine a pattern yet. In the stress test, the failure rate is low (single failure in 100,000 iterations). When I use the read method in the real application it fails 10% of the time.
Should I be coding something more like?:
Wire.requestFrom(add, 1); // request the read
/* Wait for byte to show up */
for (i=0; i<1000; i++) {
if (Wire.available() == 1) break;
}
if (i == 1000) {
// Failed
} else {
b = Wire.read();
}