22 * SBMInfo.ino
33 * Shows Smart Battery Info
44 *
5- * Copyright (C) 2016 Armin Joachimsmeyer
5+ * Copyright (C) 2016-2020 Armin Joachimsmeyer
6677 *
8+ * https://github.com/ArminJo/Smart-Battery-Module-Info_For_Arduino
9+ *
10+ * SBMInfo is free software: you can redistribute it and/or modify
11+ * it under the terms of the GNU General Public License as published by
12+ * the Free Software Foundation, either version 3 of the License, or
13+ * (at your option) any later version.
14+ *
815 * This program is distributed in the hope that it will be useful,
916 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2431#define LCD_COLUMNS 20
2532#define LCD_ROWS 4
2633
27- #define VERSION_EXAMPLE " 3.1"
34+ #define VERSION_EXAMPLE " 3.1.1"
35+
36+ /*
37+ * Version 3.1.1 - 3/2020
38+ * - Better prints at scanning.
39+ */
2840
2941// #define DEBUG
3042/*
@@ -198,13 +210,15 @@ void setup() {
198210 /*
199211 * Check for I2C device and blink until device attached
200212 */
201- if (!checkForAttachedI2CDevice (SBM_DEVICE_ADDRESS)) {
202- int tDeviceAttached;
213+ if (checkForAttachedI2CDevice (SBM_DEVICE_ADDRESS)) {
214+ sI2CDeviceAddress = SBM_DEVICE_ADDRESS;
215+ } else {
216+ Serial.println (F (" Start scanning for device at I2C bus" ));
203217 do {
204- tDeviceAttached = scanForAttachedI2CDevice ();
218+ sI2CDeviceAddress = scanForAttachedI2CDevice ();
205219 delay (500 );
206220 TogglePin (LED_BUILTIN);
207- } while (tDeviceAttached < 0 );
221+ } while (sI2CDeviceAddress < 0 );
208222 }
209223
210224 uint16_t tVoltage;
@@ -256,22 +270,23 @@ void TogglePin(uint8_t aPinNr) {
256270
257271bool checkForAttachedI2CDevice (uint8_t aStandardDeviceAddress) {
258272 Wire.beginTransmission (aStandardDeviceAddress);
259- uint8_t tOK = Wire.endTransmission ();
260- if (tOK == 0 ) {
273+ uint8_t tRetCode = Wire.endTransmission ();
274+ if (tRetCode == 0 ) {
261275 Serial.print (F (" Found attached I2C device at 0x" ));
262276 Serial.println (aStandardDeviceAddress, HEX);
263277 Serial.flush ();
264- sI2CDeviceAddress = SBM_DEVICE_ADDRESS;
265278 return true ;
266279 } else {
267280 Serial.print (F (" Transmission error code=" ));
268- Serial.println (tOK);
281+ Serial.print (tRetCode);
282+ Serial.print (F (" while looking for device at default address 0x" ));
283+ Serial.println (aStandardDeviceAddress, HEX);
269284 return false ;
270285 }
271286}
272287
273- int sScanCount = 0 ;
274288int scanForAttachedI2CDevice (void ) {
289+ int sScanCount = 0 ;
275290 int tFoundAdress = -1 ;
276291 for (uint8_t i = 0 ; i < 127 ; i++) {
277292 Wire.beginTransmission (i);
@@ -286,15 +301,13 @@ int scanForAttachedI2CDevice(void) {
286301 Serial.print (F (" Scan found no attached I2C device - " ));
287302 Serial.println (sScanCount );
288303 myLCD.setCursor (0 , 3 );
289- // print the number of seconds since reset:
290304 myLCD.print (" Scan for device " );
291305 myLCD.print (sScanCount );
292306 sScanCount ++;
293307 } else {
294308 // clear LCD line
295309 myLCD.setCursor (0 , 3 );
296310 myLCD.print (" " );
297- sI2CDeviceAddress = tFoundAdress;
298311 }
299312 return tFoundAdress;
300313}
0 commit comments