Accellstepper, one (out of 4 motors) behave a bit odd

Hey,

i have 4 steppers which runs like this 1. all run forward 2.. all reset 3. all go to a ground position 4. Motors are waiting for command which runs 1 out of 3 functions.

The 3 last mentioned functions makes the steppers go to various position both positive and negative. This works great except for one stepper which only goes in one direction (backwards), or if i say go to pos -200 (the current would be -200) then it first takes x-amount backwards then it starts to go forward. If I say go to pos -300 (the current would be -200) then it only goes backwards (i want it to go forward). When i print the pos it shows the correct position.

I have no problems running the motor in either dir with only using setSpeed.

Ill attach both the full code and the parts which is behaving strange. Bare in mind iim learning. :)

CODE THAT MAKES THE MOTOR BEHAVE STRANGE Motor is called 'hStepperOne'

 void setStepperPositionMoveTo() {

      hStepperTwo.run();
      hStepperThree.run();
      hStepperOne.run();
      vStepperOne.run();
      vStepperTwo.run();
      vStepperThree.run();
  

  }
  void runSteg(){

   if (sonarValue == 49){
     stegOne();
    } 
   if (sonarValue == 50){
     stegTwo();
    } 
   if (sonarValue == 51){
     stegThree();
    } 
  }
  
  void stegOne(){

     hStepperOneNewMotorPos = -300;
     hStepperTwoNewMotorPos = -300;
     hStepperThreeNewMotorPos = 300;
     vStepperOneNewMotorPos = 300;
     vStepperTwoNewMotorPos = -300;
     vStepperThreeNewMotorPos = -300;
     hStepperOne.moveTo(hStepperOneNewMotorPos);
     hStepperTwo.moveTo(hStepperTwoNewMotorPos);
     hStepperThree.moveTo(hStepperThreeNewMotorPos);
     vStepperOne.moveTo(vStepperOneNewMotorPos);
     vStepperTwo.moveTo(vStepperTwoNewMotorPos);
     vStepperThree.moveTo(vStepperThreeNewMotorPos);
    
     setStepperPositionMoveTo(); 
     Serial.print("Steg 1 h1 ");
     Serial.println(hStepperOne.currentPosition());
     
  } 

  void stegTwo(){

     hStepperOneNewMotorPos = -700;
     hStepperTwoNewMotorPos = -700;
     hStepperThreeNewMotorPos = 700;
     vStepperOneNewMotorPos = 700;
     vStepperTwoNewMotorPos = -700;
     vStepperThreeNewMotorPos = -700;
     hStepperOne.moveTo(hStepperOneNewMotorPos);
     hStepperTwo.moveTo(hStepperTwoNewMotorPos);
     hStepperThree.moveTo(hStepperThreeNewMotorPos);
     vStepperOne.moveTo(vStepperOneNewMotorPos);
     vStepperTwo.moveTo(vStepperTwoNewMotorPos);
     vStepperThree.moveTo(vStepperThreeNewMotorPos);

    setStepperPositionMoveTo(); 
    Serial.print("Steg 2 h1 ");
    Serial.println(hStepperOne.currentPosition());
    

  }   
   void stegThree(){

     hStepperOneNewMotorPos = -500;
     hStepperTwoNewMotorPos = -500;
     hStepperThreeNewMotorPos = 500;
     vStepperOneNewMotorPos = 500;
     vStepperTwoNewMotorPos = -500;
     vStepperThreeNewMotorPos = -500;  
     hStepperOne.moveTo(hStepperOneNewMotorPos);
     hStepperTwo.moveTo(hStepperTwoNewMotorPos);
     hStepperThree.moveTo(hStepperThreeNewMotorPos);
     vStepperOne.moveTo(vStepperOneNewMotorPos);
     vStepperTwo.moveTo(vStepperTwoNewMotorPos);
     vStepperThree.moveTo(vStepperThreeNewMotorPos);
     
    setStepperPositionMoveTo(); 
         Serial.println("h1");
         Serial.println(hStepperOne.currentPosition());
          Serial.println("steg3");


       }

FULL CODE (had to remove alot since the charachter limit, it only include 2 steppers)

/* //////////////
Stepper Setup 
*//////////////
  void stepperSetSpeed () {
    hStepperOne.setMaxSpeed(300);
    hStepperOne.setAcceleration(1000);
    hStepperTwo.setMaxSpeed(300);
    hStepperTwo.setAcceleration(1000);

    vStepperOne.setMaxSpeed(300);
    vStepperOne.setAcceleration(1000);
    vStepperTwo.setMaxSpeed(300);
    vStepperTwo.setAcceleration(1000);
    
    stepperSetSpeedPos = true;
  }
  

  
/* //////////////
Startup Run all motors full throttle!
*//////////////
  void stegStart () {           
    //Stanna respektive motor då resp brytare trycks in
     if (digitalRead (hBrytareOne) == HIGH) {
       hStepperOne.runSpeed();   
     //  Serial.print("stepper1");
      }      
      if (digitalRead (hBrytareTwo) == HIGH) {
          hStepperTwo.run();
      }      
      if (digitalRead (hBrytareThree) == HIGH) {
       hStepperThree.runSpeed(); 
      }    
    
   // If all switches are LOW 
      if(digitalRead(brytareArray[0]) == LOW) 
      {
          startState = false;
          resetMotorPos ();
           Serial.print("brytare");
  
      }
  }       
/* //////////////
 resetMotorPos  Set 0 pos al motors
*//////////////
  void resetMotorPos () {           

    hStepperOne.setCurrentPosition(0);
    hStepperTwo.setCurrentPosition(0);
    switchState = HIGH;
    Serial.print("reset");
    Serial.print(hStepperOne.currentPosition());
    
    stepperSetSpeed ();
   stegGroundPos();
   delay(200);
    }      

/* //////////////
 Run allmotors to groundPos
*//////////////
  void stegGroundPos () {
    allMotorsReset = true;    
    hStepperOneCurrentMotorPos = hStepperOne.currentPosition();
    hStepperTwoCurrentMotorPos = hStepperTwo.currentPosition();

    hStepperOneNewMotorPos = -400;
    hStepperTwoNewMotorPos = -400;

  setStepperPosition(); /Update Allmotors
  Serial.println(hStepperOneCurrentMotorPos);

    Serial.println("kör mot grundpos");
// when allmotors are at ground pos, scan for value
    if (hStepperOneCurrentMotorPos == hStepperOneNewMotorPos && 
      //  hStepperTwoCurrentMotorPos == hStepperTwoNewMotorPos 
 ) {
          switchState = LOW;
          allMotorsReset = false;
          Serial.println("GRUNDPOS delay");
          delay (2000);
          Serial.println("Börja leta efter sensor-info");
          sonar = true;
          sonarValue = random(1, 4); 
     }
  }
  /* //////////////
Reset all 
*//////////////*
  void setStepperPosition() {
// Update all motors to new pos

   if(hStepperOneCurrentMotorPos != hStepperOneNewMotorPos)  {
      hStepperOne.runSpeed();    
      hStepperOne.move(hStepperOneNewMotorPos);
    }      
   if(hStepperTwoCurrentMotorPos  != hStepperTwoNewMotorPos)  {
      hStepperTwo.runSpeed();    
      hStepperTwo.move(hStepperTwoNewMotorPos);

  }
    /* //////////////
Move to 'Steg X'
*////////////  
  void setStepperPositionMoveTo() {

      hStepperTwo.run();
      hStepperThree.run();
      hStepperOne.run();
      vStepperOne.run();
      vStepperTwo.run();
      vStepperThree.run();
  

  }
  void runSteg(){

   if (sonarValue == 49){
     stegOne();
    } 
   if (sonarValue == 50){
     stegTwo();
    } 
   if (sonarValue == 51){
     stegThree();
    } 
  }
  
  void stegOne(){

     hStepperOneNewMotorPos = -300;
     hStepperTwoNewMotorPos = -300;
     hStepperOne.moveTo(hStepperOneNewMotorPos);
     hStepperTwo.moveTo(hStepperTwoNewMotorPos);
    
     setStepperPositionMoveTo(); 
     Serial.print("Steg 1 h1 ");
     Serial.println(hStepperOne.currentPosition());
     
  } 


/* //////////////
Void LOOOOP 
*//////////////

  void loop() {

    if(startState == true) {
        if (stepperSetSpeedPos == false)
       { 
         stepperSetSpeed();
       }
      Serial.println("startstate");
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
        stegStart();
        
       }
     }
      //reset all motors 
     else if ( allMotorsReset == true && switchState==HIGH) {   
       unsigned long currentMillis = millis();
         if(currentMillis - previousMillis > interval) {
            previousMillis = currentMillis;
            stegGroundPos();
         }
      }       
       //Get sonar value, 
       else if ( sonar == true && 
         digitalRead(brytareArray[3]) == HIGH &&
         digitalRead(brytareArray[5]) == HIGH
         ) {   
     //  Serial.println("sonar körs");
         unsigned long currentMillisRand = millis();
           if(currentMillisRand - previousMillisRand > intervalRand) {
              previousMillisRand = currentMillisRand;
            /* sonarValue = 2; // random(1,4);
              Serial.println("sonarValue");
              Serial.println(sonarValue);
*/
               if (Serial.available() > 0) {
                  sonarValue = Serial.read();
                  Serial.println(sonarValue, DEC);
              }

           }
         unsigned long currentMillisRun = millis();
          if(currentMillisRun - previousMillisRun > interval) {
            previousMillisRun = currentMillisRun;
            runSteg(); 
          }
        }     
      //If any switch is LOW  
       else if ( sonar == true && 
         digitalRead(brytareArray[0]) == LOW || 
         digitalRead(brytareArray[1]) == LOW ||
         digitalRead(brytareArray[2]) == LOW || 
         digitalRead(brytareArray[3]) == LOW || 
         digitalRead(brytareArray[4]) == LOW || 
         digitalRead(brytareArray[5]) == LOW ) {    
         startState = true;
         stepperSetSpeedPos = false;

        }       
    }

If I say go to pos -300 (the current would be -200) then it only goes backwards (i want it to go forward).

Perhaps you should use terms like clockwise and counter-clockwise, since motors don't actually go forwards or backwards.

If the current position is -200, and you say to go to -300, I would expect the motor to rotate counter-clockwise (backwards). I don't see why you expect it to go clockwise (forward).

   if (sonarValue == 49){
     stegOne();
    } 
   if (sonarValue == 50){
     stegTwo();
    } 
   if (sonarValue == 51){
     stegThree();
    }

At most, one of these can be executed. There should be some else if's in there.

Are 49, 50 and 51 supposed to be '1, '2', and '3'? If so, why not make that clear? Use '1', '2', and '3' in place of 49, 50, and 51.

FULL CODE (had to remove alot since the charachter limit, it only include 2 steppers)

You need to post all of your code, using the Additional Options link below the text entry window, attaching the file.

If the current position is -200, and you say to go to -300, I would expect the motor to rotate counter-clockwise (backwards). I don’t see why you expect it to go clockwise (forward).

Sorry my mistake, mixing up terms.

   if (sonarValue == 49){

stegOne();
   }
  if (sonarValue == 50){
    stegTwo();
   }
  if (sonarValue == 51){
    stegThree();
   }




At most, one of these can be executed. There should be some else if's in there.

Ahh of course, strange that it works, tho. Will change that!

Are 49, 50 and 51 supposed to be '1, ‘2’, and ‘3’? If so, why not make that clear? Use ‘1’, ‘2’, and ‘3’ in place of 49, 50, and 51.

I made a fast test with serial read, then i got 49, 50 ,51 when typing 1,2,3 :relaxed:

Ill attach the code! There are probably alot that could be done smarter but i tried to keep it ‘clean’… :blush:

EDIT
I didn’t change the ‘if else’ yet!

lampa_newbuild_5.ino (11.7 KB)

CODE THAT MAKES THE MOTOR BEHAVE STRANGE

Which value are you sending (1, 2, or 3) that causes the strange behavior?

All of 1,2,3 makes 'hStepperOne.run();'(only hStepperOne) go counter-clockwise. When there are bigger steps eg. from -700 to -100 (here i want it going clockwise) it starts by going counter-clockwise then after x amount of steps it goes clockwise. when printing current pos it goes from -700 to -100.

How many currentMillis variables do you have in loop()? You only need one!

Your code is hard to read. Putting each { on a new line, and using Tools + Auto Format would make it easier.

But, what I think you need to do is to determine if you have a hardware of a software problem. Disconnect everything but one stepper. Write a sketch that simply steps 1000 times in one direction, then stops for 10 seconds, then steps 1000 times in the other direction. Does each stepper behave correctly when connected to the same pair of pins (or, rather, when the driver board is connected to the same set of pins? If so, then it's a software problem. If not, it's a hardware problem.

Great, thanks for all tips. ill try this by tomorrow!

Regards

If you physically swap the wiring of a 'bad' and 'good' motor, does the problem stay with the motor or stay with the Arduino outputs?