Skip to content

Commit 96b6ae7

Browse files
FischerMoseleyfischermoseley
FischerMoseley
andcommitted
Update Example6_ChangeI2CAddress.ino
Co-Authored-By: Fischer Moseley <[email protected]>
1 parent f6614fb commit 96b6ae7

File tree

1 file changed

+45
-80
lines changed

1 file changed

+45
-80
lines changed

examples/Example6_ChangeI2CAddress/Example6_ChangeI2CAddress.ino

Lines changed: 45 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,106 +22,71 @@ QwiicButton device;
2222
void setup(){
2323
Serial.begin(115200);
2424
Wire.begin(); //Join I2C bus
25-
Wire.setClock(400000); //Set I2C clock speed to 400kHz
25+
Wire.setClock(400000);
26+
device.begin(DEFAULT_BUTTON_ADDRESS); // Initialize our button! Set to DEFAULT_SWITCH_ADDRESS if you're using a
27+
// switch, or whatever the I2C address of your device is
2628

2729
//check if button will acknowledge over I2C
28-
if(device.isConnected()){
30+
if(device.isConnected()) {
2931
Serial.println("Device will acknowledge!");
3032
}
3133

3234
else {
3335
Serial.println("Device did not acknowledge! Freezing.");
3436
while(1);
3537
}
36-
}
37-
38-
void loop() {
39-
//print an introduction
40-
Serial.println("Howdy stranger! This configurator will help you change");
41-
Serial.println("The I2C address of your attached Qwiic Button/Switch.");
42-
Serial.println();
43-
Serial.println("Also, make sure that your Line Ending in the Serial Monitor");
44-
Serial.println("is set to 'Both NL & CR'");
4538
Serial.println();
46-
Serial.println("To begin, let's scan for a device. Disconnect all other Qwiic or");
47-
Serial.println("I2C devices from your microcontroller, and then send any character");
48-
Serial.println("to begin the scan.");
49-
50-
while(!Serial.available()); //wait for the user to send a character
51-
while(Serial.available()) Serial.read(); //flush the readbuffer
52-
53-
Serial.println("Beginning scan...");
54-
uint8_t address = scanForDevices();
55-
56-
if(address == -1) { //if the function returned with error, freeze
57-
Serial.println("No devices found! Freezing.");
58-
while(1);
59-
}
60-
61-
//if we got to here, it means that we haven't frozen, so we print that we found a device
62-
Serial.print("Device found at address: 0x");
63-
Serial.println(address, HEX);
64-
65-
device.begin(address);
66-
67-
//Inform the user that they'll have to pick out a new address
68-
Serial.println();
69-
Serial.println("Enter a new I2C address for the Qwiic Button to use (in hex)!");
39+
Serial.println("Enter a new I2C address for the Qwiic Button/Switch to use!");
7040
Serial.println("Don't use the 0x prefix. For instance, if you wanted to");
7141
Serial.println("change the address to 0x5B, you would enter 5B and press enter.");
42+
Serial.println();
43+
Serial.println("One more thing! Make sure your line ending is set to 'Both NL & CR'");
44+
Serial.println("in the Serial Monitor.");
45+
Serial.println();
46+
}
7247

73-
while(!Serial.available()); //wait until the user sends some characters
74-
75-
//Read the buffer and parse it for a valid hex address
76-
String stringBuffer = Serial.readStringUntil('\r');
77-
char charBuffer[10];
78-
stringBuffer.toCharArray(charBuffer, 10);
79-
uint8_t newAddress = 0;
80-
uint8_t success = sscanf(charBuffer, "%x", &newAddress);
81-
82-
//if precisely 1 valid hex number was found in the string, begin setting address
83-
if(success == 1) {
84-
//check that the address is valid
85-
if(newAddress > 0x08 && newAddress < 0x77) {
86-
Serial.println("Character recieved, and device address is valid!");
87-
Serial.print("Attempting to set device address to: ");
88-
Serial.println(newAddress, HEX);
89-
90-
device.setI2Caddress(newAddress);
91-
delay(10); //give the button/switch some time to restart its I2C hardware
92-
93-
if(device.isConnected()) {
94-
//Job is done, print that we're finished
95-
Serial.print("Address successfully changed! Device will respond at: ");
96-
Serial.println(device.getI2Caddress(), HEX);
97-
98-
Serial.println();
99-
Serial.println("Job done, freezing.");
100-
while(1);
48+
void loop(){
49+
//check if button is pressed, and tell us if it is!
50+
if(Serial.available()) {
51+
uint8_t newAddress = 0;
52+
String stringBuffer = Serial.readStringUntil('\r');
53+
char charBuffer[10];
54+
stringBuffer.toCharArray(charBuffer, 10);
55+
uint8_t success = sscanf(charBuffer, "%x", &newAddress);
56+
57+
if(success) {
58+
if(newAddress > 0x08 && newAddress < 0x77) {
59+
Serial.println("Character recieved, and device address is valid!");
60+
Serial.print("Attempting to set device address to 0x");
61+
Serial.println(newAddress, HEX);
62+
63+
if(device.setI2Caddress(newAddress) == 0) {
64+
Serial.println("Device address set succeeded!");
65+
}
66+
67+
else {
68+
Serial.println("Device address set failed!");
69+
}
70+
71+
delay(100); //give the hardware time to do whatever configuration it needs to do\
72+
73+
if(device.isConnected()) {
74+
Serial.println("Device will acknowledge on new I2C address!");
75+
}
76+
77+
else {
78+
Serial.println("Device will not acknowledge on new I2C address.");
79+
}
10180
}
10281

10382
else {
104-
Serial.print("Address change was not successfull! Reset and try again.");
83+
Serial.println("Address out of range! Try an adress between 0x08 and 0x77");
10584
}
10685
}
10786

10887
else {
109-
Serial.println("Address out of range! Try an adress between 0x08 and 0x77");
110-
}
111-
}
112-
113-
else {
114-
Serial.print("Invalid Text! Try again.");
115-
}
116-
}
117-
118-
//returns the I2C address of the connected device, or -1 if there's no device found
119-
int16_t scanForDevices() {
120-
for(uint8_t addr = 0; addr < 127; addr++) {
121-
Wire.beginTransmission(addr);
122-
if(Wire.endTransmission() == 0) {
123-
return addr;
88+
Serial.print("Invalid Text! Try again.");
12489
}
90+
12591
}
126-
return -1;
12792
}

0 commit comments

Comments
 (0)