0
$\begingroup$

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();
    }
}
$\endgroup$
4
  • 2
    $\begingroup$ 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. $\endgroup$ Commented Nov 3, 2015 at 19:42
  • $\begingroup$ This is more like a code review question. Not much related to motor or robot control. $\endgroup$ Commented Nov 9, 2015 at 6:55
  • 1
    $\begingroup$ 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? $\endgroup$
    – Shahbaz
    Commented Jan 5, 2016 at 15:33
  • $\begingroup$ ^^ "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 $\endgroup$ Commented Feb 10, 2016 at 3:08

3 Answers 3

2
$\begingroup$

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);
$\endgroup$
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.