Using Stepmotor and Lidar at the same time

Hey :slight_smile:

i am trying to built a lidar scanner for the 2D-Mapping of a room.

i tried to run the code without the stepper motor and it works fine.
as soon as i plug in the motor the distance value is default at 1200cm.

Is the Motor overwriting the received value?

#include <SoftwareSerial.h>
#include "TFMini.h"

int delaylegnth= 25;

// Setup software serial port 
SoftwareSerial mySerial(50, 51);      // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

void setup() {
///MOTOR SETUP    
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B

//// LIDAR SETUP
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  mySerial.begin(TFMINI_BAUDRATE);

  // Step 3: Initialize the TF Mini sensor
  tfmini.begin(&mySerial);   


}

void loop() {

  double i=0;
  double degree= 0;
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();

  Serial.print(degree);
  Serial.print("° ");
  Serial.print(dist);
  Serial.println(" cm");

  // Starte while Loop bis i=400
  while (i < 400)
  {
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A

  ++i;
  uint16_t dist1 = tfmini.getDistance();
  degree = i * 0.9;
  Serial.print(degree);
  Serial.print("° ");
  Serial.print(dist1);
  Serial.println(" cm");
  
  delay(delaylegnth);

  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
 ++i;
 uint16_t dist2 = tfmini.getDistance();
degree = i * 0.9;
  Serial.print(degree);
  Serial.print("° ");
  Serial.print(dist2);
  Serial.println(" cm");
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
 ++i;
  uint16_t dist3 = tfmini.getDistance();
 degree = i * 0.9;
  Serial.print(degree);
  Serial.print("° ");
  Serial.print(dist3);
  Serial.println(" cm");
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
 ++i;
  uint16_t dist4 = tfmini.getDistance();
  degree = i * 0.9;
  Serial.print(degree);
  Serial.print("° ");
  Serial.print(dist4);
  Serial.println(" cm");
  delay(delaylegnth);
  }
  ////ENDE LOOP BEI i = 400
  
  
}

´

thanks for your help

92Rockstar

What Arduino board are you using?

...R

Hey, thanks for your answers,

I am Using an Arduino Mega with Motorshield V2

I understand the confusion, i puzzled the code from 2 different codes and just added the while loop

thanks

If you are using a Mega why are you using SoftwareSerial when the Mega has 3 spare HardwareSerial ports that work much better?

...R

Robin2:
If you are using a Mega why are you using SoftwareSerial when the Mega has 3 spare HardwareSerial ports that work much better?

...R

This is my first Arduino-project, so i have no idea about Soft- or HardwareSerial Ports.
The "SoftwareSerial" code comes from an example for the LIDAR TFmini i found on github.
I didnt question it because it worked.

so can i fix my problem by using hardwareserial ports?

Delta_G:
Then it would be a good idea to do some reading about the things you don't understand before jumping off into a project this big. A LIDAR scanner is a pretty involved project and if you don't know the basics you're just going to fail and get discouraged. Take some time to do some simple stuff and learn things like what a serial port is and how many the Mega has and why it is stupid to waste processor resources on a software serial when you have a hardware serial port available.

If you don't know what a line of code does, then it doesn't belong in your program until you do.

you are right! the problem is, that i dont have enough time.
I can understand why all of you disagree with my code, but none of you answered my original question

92Rockstar:
Is the Motor overwriting the received value?

or my second question

92Rockstar:
....so can i fix my problem by using hardwareserial ports?

and if the only thing that prevents you from helping is that i "waste resources" then i am wrong to ask here.

thanks.

Delta_G:
You misunderstand.

....And now you're mad ...

No i am not, i just didnt get why you were talking about the serialport, when its not about my problem, but now i get it.

If this is your first time with Arduino, you should write your own code from the ground up and not use anything from anyone else, not even a library.

Start with blinking a light. Then see if you can get the light to blink based on things you type into the serial monitor. Then try and make the arduino do something else at the same time, like spin the stepper.

Running a stepper motor requires that the arduino use interrupts in most cases, so you need to start with the basics to get to that point.