printing out LiDAR Lite 2 data while motors are moving

Dear all,

I wired the LiDAR Lite to UNO based on the simple I2C method as described here.

I’ve tested UNO, Motor Shield and LiDAR Lite 2 individually. They work well.

Sensor readings are printing our to serial monitor continuously. When I put the motor shield and sensor together on the UNO, sensor just prints out one time on the monitor.

Please advise me how to get the reading continuously on the screen when motor is also operating.

Here is my code:

#include "MotorDriver.h"

#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;


void setup()
{
	motordriver.init();
	motordriver.setSpeed(200,MOTORB);
	motordriver.setSpeed(200,MOTORA);
  
  Serial.begin(115200);
  myLidarLite.begin();

}
 
void loop()
{
  Serial.println(myLidarLite.distance());
  motordriver.rotate(MOTOR_CLOCKWISE,MOTORA);
	delay(11500);
	motordriver.stop();
	delay(1000);
  
  motordriver.rotate(MOTOR_ANTICLOCKWISE,MOTORB);
  delay(1000);
  motordriver.stop();
  delay(3000);

  motordriver.rotate(MOTOR_ANTICLOCKWISE,MOTORA);
  delay(11500);
  motordriver.stop();
  delay(1000);

  motordriver.rotate(MOTOR_ANTICLOCKWISE,MOTORB);
  delay(1000);
  motordriver.stop();
  delay(3000);

}

Thanks a lot.

Get rid of the delay() statements. Carefully study the "Blink Without Delay" example to learn how to do so.

Thanks for your help.
Can you take a quick look on my code?
The motor would not stop!
Do I miss something?

const int motorA_1 = 8;
const int motorA_2 = 11;
const int motorB_1 = 12;
const int motorB_2 = 13;

const int motorA_speed = 9;
const int motorB_speed = 10;

int motorA_state_1 = LOW;             // MotorA state used to run motor A
int motorA_state_2 = LOW;             // MotorA state used to run motor A
int motorB_state_1 = LOW;             // MotorB state used to run motor B
int motorB_state_2 = LOW;             // MotorB state used to run motor B



#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;

unsigned long pre_ms_motorA_start = 0;        // will store last time motorA start was updated
unsigned long pre_ms_motorA_stop  = 0;        // will store last time motorA stop was updated
unsigned long pre_ms_motorB_start = 0;        // will store last time motorB start was updated
unsigned long pre_ms_motorB_stop  = 0;        // will store last time motorB stop was updated

const long interval_motorA_rotate = 11500;    // interval at which to blink (milliseconds)
const long interval_motorB_rotate = 1000;     // interval at which to blink (milliseconds)

const long interval_motorA_stop   = 2000;     // interval at which to blink (milliseconds)
const long interval_motorB_stop   = 2000;     // interval at which to blink (milliseconds)



void setup()
{
  
  pinMode(motorA_1,OUTPUT);
  pinMode(motorA_2,OUTPUT); 
  pinMode(motorA_speed,OUTPUT); 
  pinMode(motorB_1,OUTPUT); 
  pinMode(motorB_2,OUTPUT); 
  pinMode(motorB_speed,OUTPUT);

  Serial.begin(115200);
  myLidarLite.begin();

}
 
void loop()
{
 
  Serial.println(myLidarLite.distance());

  // check to see if it's time to rotate/stop motor;
  // if the difference between the current time and last time you ran
  // the motor is bigger than the interval at which you want to
  // run the motor.
  
  unsigned long now_ms_motorA_start = millis();
  if (now_ms_motorA_start - pre_ms_motorA_start >= interval_motorA_rotate) 
  {
    pre_ms_motorA_start = now_ms_motorA_start;    // save the last time you ran the motor    

    if (motorA_state_1 == LOW & motorA_state_2 == LOW)
    {
      motorA_state_1 = HIGH;//anti
      motorA_state_2 = LOW;
    } else 
    {
      motorA_state_1 == LOW;
      motorA_state_2 == LOW;
    }

    analogWrite(motorA_speed,50);
    digitalWrite(motorA_1,motorA_state_1);
    digitalWrite(motorA_2,motorA_state_2);
  }
}

The motors may be generating EMI noise that is causing the LIDAR unit controller to lose its way.

    if (motorA_state_1 == LOW & motorA_state_2 == LOW)

& should &&
Use parentheses in if statements to separate logical comparisons, and to make your intent clear.

Hello jremington. I take your comments to work on a new code. I suffered some questions about running motors in different actions. I start a new topic in programming section (more suitable place to post my questions I guess). Can you please take a look? Thank you.