/* --------------------------------------------------
 * I2C Doctor - Live Monitor Test
 * --------------------------------------------------
 * Connect at least GND, SDA & SCL of your device
 * to the I2C Doctor and navigate in the menu to
 * the Live Monitor.
 * 
 * Upload this code to your device.
 * 
 * You should now see the configured speed (I2C_SPEED)
 * as well as the packets and bytes counter increasing.
 * The targeted addresses will cycle through.
 *
 * In case of any problems, use the DIAGNOSE function
 * of your I2C Doctor :)
 * --------------------------------------------------
 */

#include <Wire.h>

#define I2C_SPEED 200000
#define DELAY_IN_MS 500

int addr = 0x08; // Start at first standard slave address (0x00-0x07 are reserved)

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(I2C_SPEED);
}

void loop() {
  /* Send a dummy byte to the current address */
  Wire.beginTransmission(addr);
  Wire.write(0xAA); // Dummy data byte
  byte err = Wire.endTransmission();

  /* Print status to Serial Monitor */
  Serial.print("Pinging Address: 0x");
  if (addr < 16) Serial.print("0");
  Serial.print(addr, HEX);
  
  if (err == 0) {
    Serial.println(" -> ACK (Device found!)");
  } else {
    Serial.println(" -> NACK (Expected if no sensor is attached)");
  }

  /* Cycle through valid 7-bit I2C addresses (up to 0x77) */
  addr++;
  if (addr > 0x77) { 
    addr = 0x08; 
  }

  delay(DELAY_IN_MS);
}
