2 steppers running at the same time

Hi! I am doing a project in whitch I need to control 2 steppers from distance but when I tried to program them to go at the same time it does not work. One goes first and the other starts right after the first finished . I will attach the code here. You may see some things that may seem a bit out place, like some comments from some of my experiments, but I am open to any suggestion. It is a 28BYJ-48 stepper. If it helps to look at it better, the command is supposed to be 4. My code may have some other issues too, but now this one is the biggest.

#include <AccelStepper.h>
#include <MultiStepper.h>


#include <LiquidCrystal_I2C.h>
#include <Wire.h>


// Pini pentru motorul 1
#define motor1Pin1 2
#define motor1Pin2 3
#define motor1Pin3 4
#define motor1Pin4 13

// Pini pentru motorul 2
#define motor2Pin1 8
#define motor2Pin2 9
#define motor2Pin3 10
#define motor2Pin4 11

#define bluetoothTx 1
#define bluetoothRx 0

// Definirea adresei I2C pentru LCD QAPASS
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2

LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);

int x=-100;

// Pini pentru modulul Bluetooth HC-05
#define bluetoothEn 6    // Pinul EN al modulului Bluetooth
#define bluetoothState 7 // Pinul STATE al modulului Bluetooth

AccelStepper motor1(AccelStepper::FULL4WIRE, motor1Pin1, motor1Pin3, motor1Pin2, motor1Pin4);
AccelStepper motor2(AccelStepper::FULL4WIRE, motor2Pin1, motor2Pin3, motor2Pin2, motor2Pin4);


//motor1.begin( motor1Pin1, motor1Pin2, motor1Pin3, motor1Pin4);


#define MAX_COMMAND_LENGTH 10 // Ajustează dimensiunea maximă a comenzilor
char receivedCommand[MAX_COMMAND_LENGTH];
int commandIndex = 0;

void setup() {

 

lcd.setBacklight(LOW);  // Setează retroiluminarea la nivel scăzut (LOW) pentru a o opri
  delay(1000);            // Așteaptă o secundă pentru a permite stabilizarea
  lcd.setBacklight(HIGH);

  Serial.begin(9600);
  pinMode(bluetoothEn, INPUT);
  pinMode(bluetoothState, OUTPUT);

   Wire.begin();
  lcd.begin(LCD_COLUMNS, LCD_ROWS, LCD_5x8DOTS);
  lcd.print("Bun venit!");

// Afișează informații suplimentare pe LCD, dacă este cazul
lcd.setCursor(0, 1);
lcd.print("Proiect Arduino");

  motor1.setMaxSpeed(1000);
  motor1.setAcceleration(100);
  motor2.setMaxSpeed(1000);
  motor2.setAcceleration(100);

  // Inițializează modulul Bluetooth
  digitalWrite(bluetoothEn, HIGH); // Activează modulul Bluetooth

  // Inițializează modulul Bluetooth HC-05
  delay(500); // Așteaptă 500 de milisecunde pentru a permite stabilizarea modulului

  // Pini pentru motorul 1
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor1Pin3, OUTPUT);
  pinMode(motor1Pin4, OUTPUT);

  // Pini pentru motorul 2
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);

  // Dezactivează motoarele inițial
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor1Pin3, LOW);
  digitalWrite(motor1Pin4, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
}

void loop() {
  updateBluetooth();

  while (Serial.available() > 0) {
    char receivedChar = Serial.read();

    if (receivedChar == '\n') {
      // S-a primit o comandă completă, procesează șirul de caractere acumulat
      receivedCommand[commandIndex] = '\0'; // Adaugă terminatorul de șir
      processBluetoothCommand(receivedCommand);
      commandIndex = 0; // Resetează indexul pentru următoarea comandă
    } else {
      // Ignoră caracterele de revenire la linie
      if (receivedChar != '\r') {
        // Adaugă caracterul la șirul de caractere acumulat
        receivedCommand[commandIndex++] = receivedChar;

        // Verifică depășirea dimensiunii maxime a comenzii
        if (commandIndex >= MAX_COMMAND_LENGTH) {
          commandIndex = 0; // Resetează indexul în caz de depășire
        }
      }
    }
  }
}

void moveMotorToPosition(AccelStepper &motor, long targetPosition) {
  motor.moveTo(targetPosition);
  motor.runToPosition();
}


void processBluetoothCommand(char* command) {
  Serial.println("Comanda primita: " + String(command)); // Afișează comanda primită


// Afișează comanda și pe LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Comanda: ");
  lcd.setCursor(0, 1);
  lcd.print(command);



 if (strcmp(command, "1") == 0) {
   motor1.runToNewPosition(1000);
   motor1.runToNewPosition(0);
   motor1.runToNewPosition(-1000);

      // Adaugă aici comenzi pentru motorul 1
  } else if (strcmp(command, "2") == 0) {
    motor1.setSpeed(5);
    motor1.runSpeed();
    motor1.runToNewPosition(0);
   motor1.runToNewPosition(500);
   //motor1.runToNewPosition(100);
    
  } else if (strcmp(command, "3") == 0) {
    motor1.setSpeed(5);
    motor1.runSpeed();
    motor1.runToNewPosition(1026);
    motor1.runToNewPosition(0);
    //motor1.moveTo(x);
  
    // Adaugă aici comenzi pentru motorul 2
  } else if (strcmp(command, "4") == 0) {
    
    //motor1.runToNewPosition(0);
    //motor2.runToNewPosition(0);
  
   //motor2.runToNewPosition(500);


   motor1.runToNewPosition(-1026);
   while (motor1.isRunning()) {
  motor2.runToNewPosition(-1026);
}
   
    
    
  
    
  } else if (strcmp(command, "A") == 0) {
    // Adaugă aici comenzi pentru ambele motoare
  } else if (strcmp(command, "B") == 0) {
    
  } else {
    // Tratează cazurile de comenzi necunoscute
    Serial.println("Comanda Bluetooth necunoscuta");
  }}
  


void updateBluetooth() {
}

this is a blocking call (cf the documentation)

that's why you don't get simultaneous movement. Each time you get a command, you wait until it's completed before fetching the next one.

you need to use run() in the loop to get the motors to move when needed.

--

side note, why do you go for Strings for the command when you have a perfect c-string... it's duplicating memory for nothing.
change

into

  Serial.print("Comanda primita: ");
  Serial.println(command); // Afișează comanda primită

if your command fits only on one character, there is no need to use strcmp()

instead of

just test the first character it will be faster

 if (command[0] == '1') {

and of course, as you have many of those, a switch/case is more appropriate

switch(command[0]) {
  case '1':
    motor1.runToNewPosition(1000);
    motor1.runToNewPosition(0);
    motor1.runToNewPosition(-1000);
    // Adaugă aici comenzi pentru motorul 1
    break;
  case '2':
    motor1.setSpeed(5);
    motor1.runSpeed();
    motor1.runToNewPosition(0);
    motor1.runToNewPosition(500);
    //motor1.runToNewPosition(100);
    break;
  case '3':
    motor1.setSpeed(5);
    motor1.runSpeed();
    motor1.runToNewPosition(1026);
    motor1.runToNewPosition(0);
    //motor1.moveTo(x);
    // Adaugă aici comenzi pentru motorul 2
    break;
  case '4':
    motor1.runToNewPosition(-1026);
    while (motor1.isRunning()) {
      motor2.runToNewPosition(-1026);
    }
    break;
  case 'A':
    // Adaugă aici comenzi pentru ambele motoare
    break;
  case 'B':

    break;
  default:
    // Tratează cazurile de comenzi necunoscute
    Serial.println("Comanda Bluetooth necunoscuta");
    break;
}

(but solve those blocking waits until you get to position)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.