Advertisement
Guest User

Untitled

a guest
Nov 30th, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include "WireScanner.h"
  2.  
  3. WireScanner::WireScanner(TwoWire &i2cPort) {
  4.   _i2cPort = &i2cPort;
  5. }
  6.  
  7. void WireScanner::scanI2C(void) {
  8.   byte error, address;
  9.   int nDevices;
  10.   Serial.println("Scanning...");
  11.   nDevices = 0;
  12.   for (address = 1; address < 127; address++) {
  13.     _i2cPort->beginTransmission(address);
  14.     error = _i2cPort->endTransmission();
  15.     if (error == 0) {
  16.       Serial.print("I2C device found at address 0x");
  17.       if (address < 16)
  18.         Serial.print("0");
  19.       Serial.println(address, HEX);
  20.       nDevices++;
  21.     } else if (error == 4) {
  22.       Serial.print("Unknown error at address 0x");
  23.       if (address < 16)
  24.         Serial.print("0");
  25.       Serial.println(address, HEX);
  26.     }
  27.   }
  28.   if (nDevices == 0)
  29.     Serial.println("No I2C devices found");
  30.   else
  31.     Serial.println("done");
  32.   Serial.println((long)&_i2cPort);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement