1

So my question is that I am trying to use a arduino library called Accel Stepper to control a stepper motor, I made code that makes the stepper motor move one rev clockwise and then one rev counter clockwise

however as i run the code, the motor has no problem going clockwise, however when it comes to reversing the shaft of the motor stays still and doesn't move while the whole motor itself vibrates, what could be my problem?

here is my code:

    // MultiStepper.pde
    // -*- mode: C++ -*-
    //
    // Shows how to multiple simultaneous steppers
    // Runs one stepper forwards and backwards, accelerating and decelerating
    // at the limits. Runs other steppers at the same time
    //
    // Copyright (C) 2009 Mike McCauley
    // $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

    #include <AccelStepper.h>
    int user = 800;
    // Define some steppers and the pins the will use

    AccelStepper stepper2(AccelStepper::DRIVER, 9,8 );
    AccelStepper stepper3(AccelStepper::DRIVER, 4, 7);

    void setup()
    {  

        stepper2.setMaxSpeed(1000);  
        stepper2.setSpeed(300);  


    }

    void loop()
    {
        // Change direction at the limits
      stepper2.moveTo(user);
      stepper2.setSpeed(300);  
      stepper2.runSpeedToPosition();   
      if (stepper2.distanceToGo() == 0){ 
        stepper2.moveTo(-stepper2.currentPosition());  
        stepper2.setSpeed(300);  
        stepper2.runSpeedToPosition(); 
      } 


    }

migrated from electronics.stackexchange.com Apr 16 '15 at 23:09

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • by the, as a stepper motor driver, I used the sparkfun easydriver – Redrachet2 Apr 16 '15 at 21:59
0

Work through your code. When it gets to the target position, your loop looks like this:

void loop()
{
    // Change direction at the limits
  stepper2.moveTo(user);
  stepper2.setSpeed(300);  
  stepper2.runSpeedToPosition();   
    stepper2.moveTo(-stepper2.currentPosition());  
    stepper2.setSpeed(300);  
    stepper2.runSpeedToPosition(); 


}

So you say, move to position 'user', then move to position '-stepper2.currentPosition'. Then as the loop comes around you again say move to position 'user', and so on.

The loop will repeat hundreds if no thousands of times per second, so you are basically telling the stepper motor to change direction thousands of times per second which will be why the motor starts vibrating when it gets to the user position.

If, just for the sake of argument, you were to move the first three lines of code in the loop() function to the setup() function, what behaviour do you think you will observe?

  • okay I see however one thing I don't understand stand is that I have a condition, it is not like I am directly telling the stepper motor to change a thousand times, I was trying to have a condition in which the stepper would reach a position then once it reaches that position it will have a new target to go to then it will run to that position, however as you have said I will try to put the first 3 lines of code in my setup function and I will observe the results – user3145648 Apr 17 '15 at 8:27
  • also anoyher concern that i have is that if i have to input code into the setup for the code in the loop to work what if i want the stepper to do a series of movments like moving from one position to another then back to its original position and moving foward again – user3145648 Apr 17 '15 at 11:17
  • You have to think about how your program goes together. What inputs do you have? When do you start the sequence of movements? Do you need to do things at timed intervals. Drawing up flow charts is very useful when it comes to learning how a program is going to go together. – Tom Carpenter Apr 17 '15 at 11:46
  • well yeah I need things to go at timed intervals since I am attemping at making a cnc gui where a user can input directions or measurements for the stepper to go this is why I need things to happen right after each other in intervals, the inputs for now are going to be simple directions because the cnc "gui" I planing to build is going to be very basic not to complicated just the arduino serial monitor I am going to use – user3145648 Apr 17 '15 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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