Two steppers not working, but time ago yes?

Hi.
Sorry for disturbing today, but I'm having some trouble with my code.
In the past I could move two steppers (not at the same time and at the same time) without any problem using the Stepper Library. But nowadays one is moving but the other stills in the place (and getting too hot).
I've tried changing the pins, the cables, the RPMs, the speed, the stepper and the driver, but still nothing.

Here it is my code:

#include <Stepper.h>
#include <Servo.h>
#include <IRremote.hpp>

Stepper motor1(200, 8, 10, 9, 11);
Stepper motor2(200, 0, 2, 1, 3);

Servo servo;

IRrecv receiver(4);

int rotacion = 0;
int angulo = 0;

void setup() {
  pinMode(13, OUTPUT);
  motor1.setSpeed(60);
  motor2.setSpeed(60);
  servo.attach(3);
  servo.write(0);
  
  receiver.begin(4, false);

  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n'); //Salto de linea
    input.trim(); // Espacios fuera
    
    int separatorIndex = input.indexOf(' '); // Valor

    if (separatorIndex != -1) {
      String command = input.substring(0, separatorIndex);
      String valueString = input.substring(separatorIndex + 1);

      int value = valueString.toInt();
      
      if (command == "Load") {
        Serial.print("Funcionando correctamente");
        servo.write(0);
        digitalWrite(13, HIGH);
        delay(200);
        digitalWrite(13, LOW);
        delay(100);
        digitalWrite(13, HIGH);
        delay(200);
        digitalWrite(13, LOW);
      }

      if (command == "stepperOne") {
        moverMotor(motor1, value, 1);
      }

      if (command == "stepperTwo") {
        moverMotor(motor2, value, 2);
      }

      if(command == "servo") {
        servo.write(value);
      }

      if (command == "soldar") {
        RestartPosition();

        for(int i = 0; i < 40; i++) {
          moverMotor(motor1, 1, 1);
          moverMotor(motor2, 1, 2);
        }
        for(int i = 0; i < 80; i++) {
          moverMotor(motor1, -1, 1);
        }
        for(int i = 0; i < 20; i++) {
          moverMotor(motor2, -1, 2);
        }
        for(int i = 0; i < 80; i++) {
          moverMotor(motor1, 1, 1);
        }

        RestartPosition();
      }
    } else {
      Serial.println("Comando ejecutado con argumentos incorrectos. Formato correcto: 'comando valor'");
    }
  }

  if (receiver.decode()) {
    uint16_t command = receiver.decodedIRData.command;
    switch(command){
      case 7:
        motor1.step(50);
        break;
      case 9:
        motor1.step(-50);
        break;
      case 64:
        motor2.step(50);
        break;
      case 25:
        motor2.step(-50);
        break;
      case 88:
        servo.write(servo.read()-5);
        break;
      case 89:
        servo.write(servo.read()+5);
        break;
      case 90:
        motor1.step(-30);
        break;
      case 91:
        motor1.step(30);
        break;
    }
    Serial.println(command);
    receiver.resume();
    delay(100);
  }
}

void RestartPosition() {
  while(rotacion != 0) {
    if(rotacion > 0) {
      motor1.step(10);
      rotacion--;
    } else {
      motor1.step(-10);
      rotacion++;
    }
  }
  while(angulo != 0) {
    if(angulo > 0) {
      motor2.step(10);
      angulo--;
    } else {
      motor2.step(-10);
      angulo++;
    }
  }
}

void moverMotor(Stepper &motor, int direccion, int motorNumber) {
  if(direccion == 1) {
    motor.step(10);
    if(motorNumber == 1) {
      rotacion++;
    } else {
      angulo++;
    }
  } else if(direccion == -1) {
    motor.step(-10);
    if(motorNumber == 1) {
      rotacion--;
    } else {
      angulo--;
    }
  }
}

I hope someone can help me and have a nice day!

Go back to earlier code and wiring and get something working. Then add another step.
Please post schematics, not Fritzing pictures".

2 Likes

You are using the same pin for two devices. Using "magic numbers" will eventually lead to your forgetting to change the numbers. Use variable names, like byte servoPin = 3

Also, are you certain pins "0" and "1" were used before? You should use two pins other than 0 and 1.

2 Likes

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