i2c_warmUp.ino example

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.
5 *
6 * @m_examplenavigation{page_extra_helper_sketches,}
7 * ======================================================================= */
8
9#include <Arduino.h>
10#include <Wire.h>
11
12int address = 0x66;
13
14uint32_t start;
15bool firstSuccess = true;
16bool firstE1 = true;
17bool firstE2 = true;
18bool firstE3 = true;
19bool firstE4 = true;
20
21int i2cStatus = 4;
22const char commands[4] = "iri";
23uint8_t index = 0;
24
25void printTime() {
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);
33}
34
35
36void setup() {
37 Wire.begin();
38
39 Serial.begin(115200);
40 while (!Serial);
41 Serial.println("I2C Warm Up Timing Test");
42}
43
44
45void loop() {
46 // Make sure we start un-powered
47 Serial.print("Wait");
48 pinMode(22, OUTPUT);
49 digitalWrite(22, LOW);
50 for (uint32_t dstart = millis(); millis() - dstart < 5000L;) {
51 Serial.print(".");
52 delay(250);
53 }
54 Serial.println(".");
55 start = millis();
56
57 // Serial.print("Attempting to write: ");
58 // Serial.println(commands[index]);
59
60 digitalWrite(22, HIGH);
61
62 bool gotResult = false;
63 while (!gotResult) {
64 Wire.beginTransmission(address);
65 Wire.write(commands[index]);
66 i2cStatus = Wire.endTransmission();
67
68 switch (i2cStatus) {
69 case 0:
70 printTime();
71 Serial.print(commands[index]);
72 Serial.println(" successfully written");
73 start = millis();
74 while (true) {
75 Wire.requestFrom(address, 40, true);
76 uint8_t code = Wire.read();
77 if (code == 1) {
78 Serial.print("Result available after ");
79 Serial.print(millis() - start);
80 Serial.print(" ms: ");
81 Serial.println(Wire.readStringUntil('\0'));
82 gotResult = true;
83 break;
84 }
85 }
86 break;
87 case 1:
88 if (firstE1) {
89 printTime();
90 Serial.println("Data is too long for transmit buffer.");
91 firstE1 = false;
92 }
93 break;
94 case 2:
95 if (firstE2) {
96 printTime();
97 Serial.println("Received NACK on transmit of address");
98 firstE2 = false;
99 }
100 break;
101 case 3:
102 if (firstE3) {
103 printTime();
104 Serial.println(" Received NACK on transmit of data");
105 firstE3 = false;
106 }
107 break;
108 case 4:
109 default:
110 if (firstE4) {
111 printTime();
112 Serial.println("Unknown error occurred");
113 firstE4 = false;
114 }
115 break;
116 }
117 // Serial.println("resending");
118 }
119
120 Serial.print("Moving to next character - ");
121 index++; // go to next character
122 if (index == 3) index = 0; // reset
123}