How to control stepmotor (tb6600) and check sensors in the same time?

I have a problem with stepmotor working when I check sensors in loop or via millis() method it stops or lower the velocity or only bumping. My code:

// Define stepper motor connections:
#define dirPin 14
#define stepPin 27

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 15 on the ESP32
#define ONE_WIRE_BUS 15

#include <Adafruit_ADS1X15.h>

Adafruit_ADS1115 ads;     /* Use thi for the 12-bit version */

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// variable to hold device addresses
DeviceAddress Thermometer;

int deviceCount = 0;
uint8_t sensor1[8] = {0x28, 0x93, 0x74, 0x31, 0x00, 0x00, 0x00, 0x43};
uint8_t sensor2[8] = {0x28, 0xA4, 0xDA, 0x76, 0x00, 0x00, 0x00, 0xDC};
uint8_t sensor3[8] = {0x28, 0x96, 0x0C, 0x77, 0x00, 0x00, 0x00, 0x12};
uint8_t sensor4[8] = {0x28, 0x93, 0x74, 0x31, 0x00, 0x00, 0x00, 0x43};

float tempSensor1, tempSensor2, tempSensor3, tempSensor4;

  // These four lines result in 1 step:
  int value = 200;
  // Im wyższa częstotliwość tym więcej amper, im mniejsza tym szybciej idzie

float voltage;

void setup() {

  Serial.begin(115200);

  ads.begin();

  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);

  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  // Set the spinning direction CW/CCW:
  digitalWrite(dirPin, HIGH);

  // Start up the library
  sensors.begin();

}

void loop() {

  int16_t adc0;

  digitalWrite(stepPin, HIGH);
  delayMicroseconds(value);

  digitalWrite(stepPin, LOW);
  delayMicroseconds(value);

  temperature();

  adc0 = ads.readADC_SingleEnded(0);

  Serial.println(((adc0 * 0.1875)/1000),3);

}

void temperature() {
  sensors.requestTemperatures();
  tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
  tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
  tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
  tempSensor4 = sensors.getTempC(sensor4); // Gets the values of the temperature
  Serial.println(tempSensor1);
  Serial.println(tempSensor2);
  Serial.println(tempSensor3);
  Serial.println(tempSensor4);
}

What can I do to get sensor values and also have continous step motor driving? I want to change step motor speed depending on readed values but not make to stop step motor, it need to work always, continously.

Sorry for my English. Thanks in advance. I think it is only programming problem, which can be solved by some code, but I don't know how to do it

If you use a library like the MobaTools stepper library you can have the motors move in the background while monitoring sensors and doing other things. For instance tell the stepper to move to a certain position (stepper.moveTo(position);) and periodically check to see if it has arrived (if(stepper.distanceToGo() == 0))while continuing with other functions. If it has arrived, issue the next instruction.

thanks, I found it before (AccelStepper library), but I was afraid of that when it go to that position it will stop for eg. 1 ms so it will be not precise? Or maybe it will not affect the result?

Hi,

Try it, experiment.

Tom.. :grinning: :coffee: :australia: :+1:

I found your german library can work. But there is a problem with this library:

const int STEPS_REVOLUTION = 6400;
MoToStepper stepper( STEPS_REVOLUTION, STEPDIR );

I defined it, cause I set 6400 steps per rotate on tb6600 is the STEPDIR correct in this set up:
(from library)

// defines for the stepper motor
#define NOSTEP      0   // invalid-flag
// The numbers for FULLSTEP and HALFSTEP must not be changed!
#define HALFSTEP    1	// in HALFSTEP steps are counted in increments of 1 ( same like STEPDIR )
#define FULLSTEP    2	// in FULLSTEP the steps are  counted in increments of 2
#define A4988       3   // using motordriver A4988
#define STEPDIR		3	// all motordrivers with a step und dir input ( same as A4988 )

I set a posiotion:

int position = 1000000;

in setup attached:

  stepper.attach( stepPin, dirPin );
  stepper.setSpeed(500);
  stepper.setRampLen( STEPS_REVOLUTION / 2);

stepper.moveTo(position);

What that RampLen mean? Is this some german?

and loop:

void loop() {

  int16_t adc0;

  if(stepper.distanceToGo() == 0){

    position += 100;
    stepper.moveTo(position);

  }

  temperature();

  adc0 = ads.readADC_SingleEnded(0);

  Serial.println(((adc0 * 0.1875)/1000),3);

}

The stepper not working with this. What do you mean by moveTo and check?

Now I set it like this (setup section):

  stepper.attach( stepPin, dirPin );
  stepper.setSpeed(100);
  stepper.setRampLen( STEPS_REVOLUTION / 2);
  stepper.rotate(1);

And it go forward, accelerate and starts going fast forward and backward (staying in same place). What happened?

That is the correct constructor if using the TB6600 driver.

In C++ each data type can contain a specific range of values. The int or uint16_t data type can hold values from -32768 to 32767. A value of 1000000 should be of the long or uint32_t data type.

That sets the speed to 50 RPM. The setSpeed() function is in RPM * 10. If you want to set the speed in steps per second use setSpeedSteps().

Ramplen defines the acceleration in steps. It is covered in the documentation (English version).

When you post code, please post the complete program, not snippets. Snippets leave out important information.

The moveTo() function will move to an absolute position. That means that if the stepper is at zero position (home) and you say stepper.moveTo(100); the stepper will rotate 100 steps clockwise. Then if you say stepper.moveTo(100); again, nothing will happen since the stepper is already at position 100. If you want to move a number of steps for each call use the move() function. The move function is relative movement. This is covered in the docs, as well.

When you issue a move() or moveTo() function the motor will move to the indicated position "in the background". So if you say stepper.moveTo(100); the motor will do that with no further intervention from the user. If you want another motion once the motor reaches 100 you can check to see if it has reached that position before issuing another command.

Again, please post complete programs, not snippets.

My full code:

#include <MobaTools.h>

// Define stepper motor connections and motor interface type. 
#define dirPin 14
#define stepPin 27

const int STEPS_REVOLUTION = 6400;
MoToStepper stepper( STEPS_REVOLUTION, STEPDIR ); 

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 15 on the ESP32
#define ONE_WIRE_BUS 15

#include <Adafruit_ADS1X15.h>

Adafruit_ADS1115 ads;     /* Use thi for the 12-bit version */

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// variable to hold device addresses
DeviceAddress Thermometer;

int deviceCount = 0;
uint8_t sensor1[8] = {0x28, 0x93, 0x74, 0x31, 0x00, 0x00, 0x00, 0x43};
uint8_t sensor2[8] = {0x28, 0xA4, 0xDA, 0x76, 0x00, 0x00, 0x00, 0xDC};
uint8_t sensor3[8] = {0x28, 0x96, 0x0C, 0x77, 0x00, 0x00, 0x00, 0x12};
uint8_t sensor4[8] = {0x28, 0x93, 0x74, 0x31, 0x00, 0x00, 0x00, 0x43};

float tempSensor1, tempSensor2, tempSensor3, tempSensor4;

// Im wyższa częstotliwość tym więcej amper, im mniejsza tym szybciej idzie

float voltage;

int position = 1000000;

void setup() {

  Serial.begin(115200);

  ads.begin();

  stepper.attach( stepPin, dirPin );
  stepper.setSpeed(100);
  stepper.setRampLen( STEPS_REVOLUTION / 2);
  stepper.rotate(1);

  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);

  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  // Set the spinning direction CW/CCW:
  digitalWrite(dirPin, HIGH);

  // Start up the library
  sensors.begin();

//stepper.moveTo(1000);

}



void loop() {

  int16_t adc0;

  // if(stepper.distanceToGo() == 0){

  //   position += 2000;
  //   stepper.moveTo(position);

  // }

  temperature();

  adc0 = ads.readADC_SingleEnded(0);

  Serial.println(((adc0 * 0.1875)/1000),3);

}

void temperature() {
  sensors.requestTemperatures();
  tempSensor1 = sensors.getTempC(sensor1); // Gets the values of the temperature
  tempSensor2 = sensors.getTempC(sensor2); // Gets the values of the temperature
  tempSensor3 = sensors.getTempC(sensor3); // Gets the values of the temperature
  tempSensor4 = sensors.getTempC(sensor4); // Gets the values of the temperature
  Serial.println(tempSensor1);
  Serial.println(tempSensor2);
  Serial.println(tempSensor3);
  Serial.println(tempSensor4);
}

Ramplen means accelaration from start to set velocity? Cause it can start 0 to eg. 500 once, but I don't need to accelerate during continous work

That is the full code that results in:
"And it go forward, accelerate and starts going fast forward and backward (staying in same place)."

Yes.

Then set rampLen to 0. But stepper cannot go from 0 to high speed instantaneously. If the stepper does not miss steps then OK.

I have no idea how that code can do what you say. The only stepper commands are commented out.

I suggest that you read the documentation and experiment with the library until you understand how it works. Don't try to integrate the temperature stuff until you have an understanding of the library.

Okay thanks I found an issue:
I have predefinied rotation (pin connection DIR- to GND) so it need to be:
stepper.rotate(-1);

Thanks for library, it is really useful.