File tree 2 files changed +22
-11
lines changed
examples/Example4_ChangeAddress
2 files changed +22
-11
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ void setup()
66
66
// Call change address.....
67
67
sfeTkError_t err = myUltrasonic.changeAddress (newAddr);
68
68
69
+
69
70
if (err)
70
71
{
71
72
while (1 )
@@ -75,16 +76,28 @@ void setup()
75
76
delay (1000 );
76
77
}
77
78
}
78
- delay (1000 );
79
79
80
- Serial.print (" Load up example 1 with the new address at: " );
81
- Serial.println (newAddr, HEX);
82
- Serial.println (" Freezing...." );
83
- while (1 )
84
- ;
80
+ Serial.println (" Address changed successfully!" );
81
+ Serial.println (" Reading Distance at new address......" );
82
+ delay (3000 );
85
83
86
84
}
87
85
88
86
void loop ()
89
87
{
88
+ uint16_t distance = 0 ;
89
+ myUltrasonic.triggerAndRead (distance);
90
+
91
+ // Print measurement
92
+ Serial.print (" Distance (mm): " );
93
+ Serial.println (distance);
94
+
95
+ // Serial.println("Distance (cm): ");
96
+ // Serial.print((distance / 10.0), 2);
97
+
98
+ // Serial.println("Distace (in): ");
99
+ // Serial.print((distance / 25.4), 2);
100
+
101
+ // Wait a bit
102
+ delay (100 );
90
103
}
Original file line number Diff line number Diff line change @@ -86,8 +86,8 @@ sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
86
86
return kSTkErrFail ;
87
87
88
88
// We want to shift the address left before we send it.
89
- address <<= 1 ;
90
- const uint8_t toWrite[2 ] = {kUltrasonicAddressChangeCommand , address };
89
+ uint8_t tempAddress = address << 1 ;
90
+ const uint8_t toWrite[2 ] = {kUltrasonicAddressChangeCommand , tempAddress };
91
91
92
92
// Write the new address to the device
93
93
err = _theBus->writeRegion (toWrite, numBytes);
@@ -99,13 +99,11 @@ sfeTkError_t sfeQwiicUltrasonic::changeAddress(uint8_t &address)
99
99
return kSTkErrFail ;
100
100
}
101
101
102
+ _theBus->setAddress (address);
102
103
// Check whether the write was successful
103
104
if (err != kSTkErrOk )
104
105
return err;
105
106
106
- // Update the address in the bus
107
- _theBus->setAddress (address);
108
-
109
107
// Done!
110
108
return kSTkErrOk ;
111
109
}
You can’t perform that action at this time.
0 commit comments