Question about LSM6DSOX 6 DoF IMUs

Hi,

I try to use LSM6DSO captor, I follow this guide Arduino | LSM6DSOX, ISM330DHC, & LSM6DSO32 6 DoF IMUs | Adafruit Learning System
I’ve created a file captor_function.cpp, in this file I would like to put some functions to use with my captor.
For the moment I try just to find the captor. So if I follow the guide I need to use begin_I2C() function.

So in my cpp function file I’ve created a function called initiateCaptor() with one parameter the captor like that

void initiateCaptor(Adafruit_LSM6DSO32 captor);

My cpp

void initiateCaptor(Adafruit_LSM6DSO32 captor){
  SerialUSB("Begin initiate");
  if (!captor.begin_I2C()) {
    while (1) {
      delay(10);
    }
  }
  SerialUSB("Find it!");
}

Ok so this is easy. In my ino file I include my header and in my setup I called this function.

#include <Adafruit_LSM6DSO32.h>
Adafruit_LSM6DSO32 dso32;

void setup() {
  SerialUSB.begin(115200);
  while (!SerialUSB)
    delay(10);
  
   initiateCaptor(dso32);
}

void loop() {
  // put your main code here, to run repeatedly:
}

But if I do thaht I never see the message Find it! I only see Begin initiate. This is strange because if I put the while and the delay in the setup this is works I find my captor, but if I put that in a function my captor is nerver find why?
I also try to give captor by a pointer in my function same result. I have no error when the compilation stage is called. It’s look like the program stay in the while(1)…

Explain in your own words, what these lines do. I have added comment to describe it in my words.

SerialUSB.begin(115200);    // initialize the serial port over usb to the computer.
SerialUSB.println("Hello World");     // send Hello World and add a \r and \n to it.
SerialUSB("Begin initiate");      // release the kraken

If your run an example from the library, does it work ?

Which Arduino board do you use ? Do you use the Arduino IDE ?

This is the object you need to work with: Adafruit_LSM6DSO32 dso32;
If you use it as parameter, then you no longer work with the global object, but instead you work with a copy on the stack.
Either use the global object in the function (no parameter) or use a pointer or "by reference".
Try "by reference".

If you don't know if the program stays in the while(1), then make sure you know :wink:

  SerialUSB.println("I'm going in.");
  if (!captor.begin_I2C()) 
  {
    while (1) 
    {
      SerialUSB.print( ".");
      delay(100);
    }
  }
  SerialUSB.println("Yes, I got out!");