ArtemTechnologies

ATWatch - Arduino совместимая носимая платформа для обучения робототехнике и программированию на языке C++

I2C Scanner

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5.  
  6. #define BuzzerPin   15
  7. #define OLED_RESET  24
  8. #define OLED_DC     26
  9. #define OLED_CS     31
  10. Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
  11.  
  12. void setup()
  13. {
  14.   Wire.begin();
  15.   display.begin(SSD1306_SWITCHCAPVCC);
  16.   display.clearDisplay();
  17. }
  18.  
  19. void loop()
  20. {
  21.   byte error, address;
  22.   int nDevices;
  23.   display.setTextSize(0);
  24.   display.setTextColor(WHITE);
  25.   display.setCursor(0, 0);
  26.   display.println("Scanning...");
  27.  
  28.   nDevices = 0;
  29.   for (address = 1; address < 127; address++ )
  30.   {
  31.     // The i2c_scanner uses the return value of
  32.     // the Write.endTransmisstion to see if
  33.     // a device did acknowledge to the address.
  34.     Wire.beginTransmission(address);
  35.     error = Wire.endTransmission();
  36.  
  37.     if (error == 0)
  38.     {
  39.       display.print("I2C device found 0x");
  40.       if (address < 16)
  41.         display.print("0");
  42.       display.println(address, HEX);
  43.       nDevices++;
  44.     }
  45.     else if (error == 4)
  46.     {
  47.       display.print("Unknown error at address 0x");
  48.       if (address < 16)
  49.         display.print("0");
  50.       display.println(address, HEX);
  51.     }
  52.   }
  53.   if (nDevices == 0)
  54.     display.println("No I2C devices found");
  55.   else
  56.     display.println("done");
  57.   display.display();
  58.   delay(5000);           // wait 5 seconds for next scan
  59.   display.clearDisplay();
  60. }