Testing sketch to see how long an attached I2C device takes to begin to respond to commands.
Testing sketch to see how long an attached I2C device takes to begin to respond to commands.
1/** =========================================================================
2 * @example{lineno} i2c_warmUp.ino
3 * @brief Testing sketch to see how long an attached I2C device takes to
4 * begin to respond to commands.
6 * @m_examplenavigation{page_extra_helper_sketches,}
7 * ======================================================================= */
15bool firstSuccess = true;
22const char commands[4] = "iri";
26 Serial.print("I2C device replied at address 0x");
27 if (address < 16) Serial.print("0");
28 Serial.print(address, HEX);
29 Serial.print(" after ");
30 Serial.print(millis() - start);
31 Serial.print(" ms, code: ");
32 Serial.println(i2cStatus);
41 Serial.println("I2C Warm Up Timing Test");
46 // Make sure we start un-powered
49 digitalWrite(22, LOW);
50 for (uint32_t dstart = millis(); millis() - dstart < 5000L;) {
57 // Serial.print("Attempting to write: ");
58 // Serial.println(commands[index]);
60 digitalWrite(22, HIGH);
62 bool gotResult = false;
64 Wire.beginTransmission(address);
65 Wire.write(commands[index]);
66 i2cStatus = Wire.endTransmission();
71 Serial.print(commands[index]);
72 Serial.println(" successfully written");
75 Wire.requestFrom(address, 40, true);
76 uint8_t code = Wire.read();
78 Serial.print("Result available after ");
79 Serial.print(millis() - start);
80 Serial.print(" ms: ");
81 Serial.println(Wire.readStringUntil('\0'));
90 Serial.println("Data is too long for transmit buffer.");
97 Serial.println("Received NACK on transmit of address");
104 Serial.println(" Received NACK on transmit of data");
112 Serial.println("Unknown error occurred");
117 // Serial.println("resending");
120 Serial.print("Moving to next character - ");
121 index++; // go to next character
122 if (index == 3) index = 0; // reset