I2C affect Stepper speed in the main loop

Hello guys. I have a code as follow. I need some suggestion on how to improve it. My systems has 1 stepper, 1 servo motor, and 1 lidar sensor.

  static unsigned long timer = 0;
  unsigned long interval = 10; //1000/10 = 100 samples every second
  if (millis() - timer > interval)  
  {
    timer = millis();
    myLidarLite.beginContinuous(false,0x13,0xff,0x62);  //100Hz, infinite reading
    distance = myLidarLite.distanceContinuous();...

When ever I try to get more samples by changing interval, the stepper is slow down (at 20 or more samples, at interval <50).

And in addition, the ** myLidarLite.beginContinuous(false,0x13,0xff,0x62);** seems to cause the Stepper to slow down also. Here is it part in the library .cpp file

void LIDARLite::beginContinuous(bool modePinLow, char interval, char numberOfReadings,char LidarLiteI2cAddress){
  //  Register 0x45 sets the time between measurements. 0xc8 corresponds to 10Hz
  //  while 0x13 corresponds to 100Hz. Minimum value is 0x02 for proper
  //  operation.
  write(0x45,interval,LidarLiteI2cAddress);
  //  Set register 0x04 to 0x20 to look at "NON-default" value of velocity scale
  //  If you set bit 0 of 0x04 to "1" then the mode pin will be low when done
  if(modePinLow){
    write(0x04,0x21,LidarLiteI2cAddress);
  }else{
    write(0x04,0x20,LidarLiteI2cAddress);
  }
  //  Set the number of readings, 0xfe = 254 readings, 0x01 = 1 reading and
  //  0xff = continuous readings
  write(0x11,numberOfReadings,LidarLiteI2cAddress);
  //  Initiate reading distance
  write(0x00,0x04,LidarLiteI2cAddress);
}

Thank you for your time, any help is appreciated.

Tony.ino (2.61 KB)

The problem may be in the LIDARlite() library (which I have never used). How long does it need for the calls beginContinuous() and distanceContinuous()?

Also, you have a great many Serial.print() commands and they are slow. See how it performs if you comment them out.

...R

How long does it need for the calls beginContinuous() and distanceContinuous()?

I don't know for how long. The print serial doesnt slow it down
Maybe there is too many write to I2C in the beginContinous then?

Also the distanceContinous has some part use Wire.h which cause I2c hang when I work with it early on.

Sylanvys:
I don't know for how long.

You could insert some temporary code to measure it. Save the value of micros() before and after the call.

...R

Sylanvys:
Hello guys. I have a code as follow. I need some suggestion on how to improve it. My systems has 1 stepper, 1 servo motor, and 1 lidar sensor.

  static unsigned long timer = 0;

unsigned long interval = 10; //1000/10 = 100 samples every second
 if (millis() - timer > interval)  
 {
   timer = millis();
   myLidarLite.beginContinuous(false,0x13,0xff,0x62);  //100Hz, infinite reading
   distance = myLidarLite.distanceContinuous();...

stupid question:sorry I have not used this system before either but you want so sample (read distance) every 10ms... so why are you use infinite reading instead of requesting only one read every 10ms ie "myLidarLite.beginContinuous(false,0x13,0x01,0x62); " instead of "myLidarLite.beginContinuous(false,0x13,0xff,0x62);"?

either that or "myLidarLite.beginContinuous(false,0x13,0xff,0x62); //100Hz, infinite reading" should be moved to setup as this looks like an mode initialisation for the LIDAR and does not need to be called every time