// For: https://forum.arduino.cc/t/using-the-wire-readbytes/997313
#include <Wire.h>
byte buffer[20];
void setup()
{
Serial.begin( 115200);
Serial.println( "Testing timeout with a bug in the code...");
Wire.begin();
Wire.setTimeout( 3000); // default 1 second, let's make it 3 for fun.
Wire.beginTransmission( 0x68);
Wire.write( 0x00);
Wire.endTransmission();
unsigned long t1 = millis();
// Better code could be:
// Wire.requestFrom( 0x68, 5);
// if( Wire.available() == 5)
// Or shorter:
// if( Wire.requestFrom( 0x68, 5) == 5)
//
Wire.requestFrom( 0x68, 5);
if( Wire.available() > 0) // I do not agree with this code line
{
Wire.readBytes( buffer, 6); // use a bug, read 6 when there are only 5 !
}
unsigned long t2 = millis();
unsigned long elapsedMillis = t2 - t1;
Serial.print( "The code with the bug took ");
Serial.print( (float) elapsedMillis / 1000.0);
Serial.println( " seconds.");
}
void loop() {}