Stepper motor in only one way

Hi, I've a problem with my stepper motor, or with my driver or with IDE, I don't know. I've tried A4988 and tmc2208, with my arduino mega and with my cnc shield, the stepper motor only goes forward, I aso tried with 2 other stepper and they work in the same way. I don't know, I've aredy written a code with stepper motor that works with mobatools and with accelstepper and they work but now a simple code doesn't works at all and I don't know why.
I left my code below:


```cpp
// Includo le librerie necessarie
#include <MobaTools.h>

long step = 500;
// Definizione dei pin dell'Arduino
// Pin dello stepper motor

const byte enablePin = 8;
// Definizione del driver dello stepper motor
MoToStepper stepper(200, STEPDIR);

// Setup
void setup() {
  pinMode(enablePin, OUTPUT);
  Serial.begin(9600);
  stepper.attach(3, 6);
  stepper.attachEnable(enablePin, 1, LOW);
  stepper.setSpeed(100);
}

// Loop
void loop() {
  stepper.doSteps(-step);
  stepper.doSteps(step);
  delay(1000);
}

thanks to everyone that helps me, and sorry for my english

The MobaTools methods ore not blocking. So immediately after commanding the stepper to go ccw, you command it to go cw. Then it turns while the delay.
You must give the stepper time to go backwards - either by a delay, or by checking if it's still moving.

I added a deay but it still not working, can I have a problem with arduino and computer?


```cpp
#include <AccelStepper.h>

// Definisci il numero di passi per giro completo del motore
const int stepsPerRevolution = 2000;

// Collega i pin del driver del motore stepper
const int stepPin = 3;
const int dirPin = 6;

// Crea un oggetto AccelStepper
AccelStepper stepper(1, stepPin, dirPin);  // ModalitĂ  driver: 1

void setup() {
  // Imposta la velocitĂ  massima (in steps per secondo)
  stepper.setMaxSpeed(1000);

  // Imposta l'accelerazione (in steps per secondo al secondo)
  stepper.setAcceleration(200);
  stepper.setPinsInverted(true, false, true);
  // Inizializza la posizione del motore
  stepper.setCurrentPosition(0);
}

void loop() {
  // Fai girare il motore in avanti
  stepper.move(stepsPerRevolution);

  // Attendi che il motore raggiunga la posizione desiderata
  while (stepper.distanceToGo() != 0) {
    stepper.run();
  }

  // Pausa per un secondo
  delay(1000);


  // Fai girare il motore in avanti
  stepper.move(0);

  // Attendi che il motore raggiunga la posizione desiderata
  while (stepper.distanceToGo() != 0) {
    stepper.run();
  }
}

With this code it goes only forward and not backward, why?

My advice was related to your original Sketch. Where did you insert the delay?

Now you show something completely different. This sketch never changes the direction of the motor.
And please post a schematic of your wiring.

because you changed from the MobaTools-library

to the AccelStepper-library.

change it back and stay with the MobaTools-library

For making visible what your code is doing load this sketch
open the serial monitor and watch what gets printed
Then you will understand what the code really does.

// Includo le librerie necessarie
#include <MobaTools.h>

long step = 500;
// Definizione dei pin dell'Arduino
// Pin dello stepper motor

const byte enablePin = 8;
// Definizione del driver dello stepper motor
MoToStepper stepper(200, STEPDIR);

// Setup
void setup() {
  pinMode(enablePin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Setup-Start");
  stepper.attach(3, 6);
  stepper.attachEnable(enablePin, 1, LOW);
  stepper.setSpeed(100);
  Serial.println("exiting Setup");
}

// Loop
void loop() {
  Serial.println("Top of loop");
  stepper.doSteps(-step);
  Serial.println("MINUS steps stepper.doSteps(-step) executed");
  stepper.doSteps(step);
  Serial.println("PLUS steps stepper.doSteps(step) executed");
  delay(1000);
  Serial.println("delay(1000); finished");
}

best regards Stefan

YMMD :slight_smile:

Autocorrection and wrong language :flushed::joy:

1 Like

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