Stepper motor loses torque inside big code

Hi!

I’m using a stepper motor pm35s-048. I need it to move a certain mechanism (details are not necessary).

When I use a little example code to control it, it moves exactly as I need it — in torque and speed.

However, when I use my full code (that includes the use of a display and some buttons), when it reaches that part of the code the motor loses a lot of torque, so it can’t move the mechanism (but it is close to do it). The code that I include in my code is exactly the same one than the one in the little example code.

This is the little example code:

int A=6;
int B=10;
int C=13;
int D=14;
void setup(){
  pinMode(A,OUTPUT);
  pinMode(B,OUTPUT);
  pinMode(C,OUTPUT);
  pinMode(D,OUTPUT);
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
}
void loop(){
  
  digitalWrite(D,HIGH);
  digitalWrite(C,HIGH);
  delay(100);
  digitalWrite(D,LOW);
  digitalWrite(B,HIGH);
  delay(100);
  digitalWrite(C,LOW);    
  digitalWrite(A,HIGH);
  delay(100);
  digitalWrite(B,LOW);
  digitalWrite(D,HIGH);
  delay(100);
  digitalWrite(D,LOW);
  digitalWrite(A,LOW);
}

And here is some part of my full program (as you can see, this parts are exactly the same as the ones of the little example code):

// Some code
const int motorA = 6; // Motor a pasos A
const int motorB = 10; // Motor a pasos B
const int motorC = 13; // Motor a pasos C
const int motorD = 14; // Motor a pasos D

void setup() {
  
  // Setup del Motor
  pinMode(motorA,OUTPUT);
  pinMode(motorB,OUTPUT);
  pinMode(motorC,OUTPUT);
  pinMode(motorD,OUTPUT);
  digitalWrite(motorA,LOW);
  digitalWrite(motorB,LOW);
  digitalWrite(motorC,LOW);
  digitalWrite(motorD,LOW);
// Some more setup code

void loop() {
  
int readingOne = digitalRead(buttonOnePin);
int readingTwo = digitalRead(buttonTwoPin);
int readingThree = digitalRead(buttonThreePin);
int readingFour = digitalRead(buttonFourPin);

// Some other code

     if (readingFour != lastButtonFourState) {
          lastDebounceTimeFour = millis();}
       if ((millis() - lastDebounceTimeFour) > debounceDelay) {
             if (readingFour != buttonFourState) {
                       buttonFourState = readingFour;
                       if (buttonFourState == HIGH) {
                         if (flag1==1){
                           for (int vueltas=0; vueltas<12; vueltas++){
                             digitalWrite(motorD,HIGH);
                             digitalWrite(motorC,HIGH);
                             delay(100);
                             digitalWrite(motorD,LOW);
                             digitalWrite(motorB,HIGH);
                             delay(100);
                             digitalWrite(motorC,LOW);    
                             digitalWrite(motorA,HIGH);
                             delay(100);
                             digitalWrite(motorB,LOW);
                             digitalWrite(motorD,HIGH);
                             delay(100);
                             digitalWrite(motorD,LOW);
                             digitalWrite(motorA,LOW);}                  
                           }}}}
     lastButtonFourState = readingFour;

Do you know what can I do to keep the same torque? Thanks! :slight_smile:

You need to post the whole sketch as requested in the “how to post in this forum”
sticky thread. The problem could be anywhere.

Using the AccelStepper library may be easier than figuring out a DIY approach.

I suspect you need to separate the movement of the stepper from the logic of your code so that the steps are called from loop() when the time for a step has arrived using the technique in the Blink Without Delay example sketch rather than delay() to achieve the timing.

Taking almost all of the code out of loop() and putting it into short functions that each deal with a single action would probably make it easier for you to see the way your code works. It would certainly make it easier for me.

...R