# How to make a stepper motor rotate and come to a position of certain degrees (say for 90 degrees) from any initial position?

I tried this code and it worked:

void loop()
{
int y = 104;
int x2 = vertical2.currentPosition();
int z2 = y-x2;
int x1 = horizontal2.currentPosition();
int z1 = y-x1;

horizontal2.moveTo(z1);
horizontal2.run();

vertical2.moveTo(z2);
vertical2.run();
}


However, the problem is that when the above code was placed inside a loop such as if loop, it was not working. Can anyone help me solve this problem? I am using accelStepper library for the code above.

void loop()
{
int dummy=1;
if(dummy==1)
{
int y = 104;
int x2 = vertical2.currentPosition();
int z2 = y-x2;
int x1 = horizontal2.currentPosition();
int z1 = y-x1;

horizontal2.moveTo(z1);
horizontal2.run();

vertical2.moveTo(z2);
vertical2.run();
}
}

• Welcome to robotics.SE! "It's not working" is not very useful. Don't dump your code here and expect somebody to fix it or write it for you. What have you tried so far in terms of debugging? Do you get any error messages? Why do you declare your variables in the loop function? Does it make sense to set y=104 over and over again? Also:if is not a loop. – Bending Unit 22 Nov 3 '15 at 19:42
• This is more like a code review question. Not much related to motor or robot control. – Gürkan Çetin Nov 9 '15 at 6:55
• if is not a loop! Also, is this really your exact code? Or did you rewrite it here, perhaps avoiding a typo you have in your real code? – Shahbaz Jan 5 '16 at 15:33
• ^^ "if" isn't a loop, but since this is likely from an Arduino (see "void loop()" above it), the code snippet is a looping structure – Ryan Loggerythm Feb 10 '16 at 3:08

You're in luck. Stepper motors do not have absolute positions. There is no 0-degree mark, nor 90-degree mark, etc. Everything is relative to the current step, so you don't have to query where the motor is positioned.

If you want to track the absolute position, look into position encoding motors (rotary encoders are popular).

For your case, you need to research 1 thing: how many steps does my motor complete in 1 revolution? Another way of asking this is: what is the angular resolution of my motor?

This post will take you the rest of the way: Arduino and stepper control

Where you see my code saying stepperX.step(500);, that's the command to take 500 steps.

If you want x number of degrees, you'll write the following:

numSteps = stepsPerRevolution * (desiredNumberOfDegrees / 360);
stepperX.step(numSteps);


I'm guessing this is Arduino code, you might want to move it to that forum.

If this is Arduino code then I think the problem you have is you eyes are too slow. At the end of the loop function it starts the loop function again, so it appears to do nothing, because it's already in the right place.

Hope that is clear enough and helps you.

As an alternative you could use a light interruption sensor and disk as is typically found inside any old household printer. Modules can cost \$20 online, but my local thrift shop sells whole printers for \$10 all the time.

Add the ring to your shaft and add the sensor's four wires, then by counting the number of lines for a full rotation you can divide this number for precise positioning.