stepper motor: 360° rotation, backwards and forwards

Hi there! :slight_smile:

I have more of a logic question- I’m building a ‘musical instrument’, with a stepper motor located in the middle of a circle, and cups ectr are standing around it in a circle. the stepper is supposed to move to each cup, and then hits it (im leaving out the specifics of this bc its not needed to make it simpler). so the stepper motor needs to move backwards and forwards, and still reach the correct destination every time.

there are 8 cups, so the stepper motor has to move 45° to reach a new cups.

the bug ive found is that, when i’m playing the note at 180° and 270°, the stepper moves likes this: 180-270-0-90-180-270… when it should just be moving like this: 180-270-180-270…

my function looks like this:

void moveHere(int target) {
//determines where the stepper has to move in order to reach its target - depending on where it is currently
//first we check whether we have to move at all
if (target == previousPos) {
        rotationDistance = 0;
  //then we check where we were last
  //ive seperated the cases (previousPos <= 180) and (previousPos >180) so that the stepper motor uses the most direct way
} else if (previousPos <= 180) {
        if (target <= 180) {
        rotationDistance = target - previousPos;   
      } else if (target > 180) {
        rotationDistance = 360 - target + previousPos;
      }
} else if (previousPos > 180) {
        if (target <= 180){
        rotationDistance = 360 - previousPos + target;  
      } else if (target > 180) {
        rotationDistance = target - previousPos;   
      }
} 
      return rotationDistance;
}

so I’m guessing I’ve gotten some of my calculation wrong :smiley: but I can’t figure out where the mistake is…

if you want me to post the rest of my code, pls ask^^

thank you!!

You can't return a value from a void function. By saying "void" you are telling the compiler that you don't intend to return a value. I am surprised that this compiles at all.

I think you will make life a lot easier for yourself if you work with numbers of steps rather than degrees. Then, if you have a variable that holds the current step position you can figure out how many steps are needed to get to a new position. For example if the current position is 100 (180 deg for a 200 step motor) and you want it to go to position 50 (90 deg) the number of steps to move is newPosition - currentPosition

...R Stepper Motor Basics Simple Stepper Code also look up the AccelStepper library

@MorganS thank you, don’t know how i missed that^^ I’ve changed it to int now (it did work with void though, mysteriously <.<)

@Robin2 I’ve cut the function down to this now and it seems to work… so i think I’ll stick to this…

int moveHere(int target) {
    //determines where the stepper has to move in order to reach its target - depending on where it is currently
    //first we check whether we have to move at all
    if (target == previousPos) {
      rotationDistance = 0;
      //then we check where we were last
    } else {
      rotationDistance = target - previousPos;
    }
     return rotationDistance;  
  }

also, I’m using the StepperDriver Library from here: https://github.com/laurb9/StepperDriver so far, it works pretty well!

thank you for the feedback!!