Basic questions about using AccelStepper.h

13,033 views
Skip to first unread message

Xing Li

unread,
Jul 16, 2013, 3:09:44 AM7/16/13
to accels...@googlegroups.com
Hi,

I am very new to AccelStepper.h, and I am trying to use this library to control one bipolar stepper motor to move to a certain position, with constant speed, and stop. The motor is 1.8degrees/step, and I used Big Easy Driver full step mode for hardware connection; Here is my code:

#include <AccelStepper.h>

int pinSTEP=3;
int pinDIR=5;

AccelStepper stepper(1, pinSTEP, pinDIR);

void setup()
{
stepper.setMaxSpeed(1000);  
stepper.setSpeed(100);
stepper.move(2000);
}

void loop()
{
   stepper.run();
}


The motor seemed moved once, then it didn't move at all. I tried the same hardware connection with my other codes, the motor moved as I expected, so I think there should be sth wrong with my sketch; Particularly, I am confused by
stepper.move(distance), and stepper.moveTo(targetPosition).

I suppose I should specify motor's current position, so that it could move to an absolute/relative new position? If so, how should I do that?

Also, what's the unit in stepper.move(), and stepper.moveTo()? Thanks!

Best,
Sonia

Sandy Noble

unread,
Jul 16, 2013, 3:28:55 AM7/16/13
to accels...@googlegroups.com
The motor always starts off at 0 at power on.  move() and moveTo() are a relative and absolute version of the command to set a target position.  The unit is "motor steps".

stepper.move(5000) will set the target for 5000 steps in one direction (a relative move), so if it is currently at position 2000, it will move to 7000.
stepper.moveTo(5000) will set the target to position 5000 (absolute position), so if it is currently at position 2000 it will move 3000 extra steps to get to position 5000.

If you want a constant speed, then you might use .runSpeedToPosition(<position>).   This takes an absolute position as an argument, and runs at the last set speed.  If you want to move to a relative position you can do something like

stepper.setSpeed(500);
stepper.runSpeedToPosition(stepper.currentPosition()+2000);

and of course you can reset the position at any point to any value with stepper.setCurrentPosition(2000).

Also, if you want a constant speed, you might dispense with accelstepper (it's about accelerations after all), and just use the basic Arduino Stepper library.

sn


--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Sandy Noble

Ramazan Yılmaz

unread,
Dec 27, 2015, 10:17:39 AM12/27/15
to accelstepper
hi , i have a arduino problem , can you help me ??

16 Temmuz 2013 Salı 10:28:55 UTC+3 tarihinde Sandy Noble yazdı:

Sandy Noble

unread,
Dec 27, 2015, 2:44:07 PM12/27/15
to accels...@googlegroups.com
You could start with the arduino site (https://www.arduino.cc/), and come back here when you've got an accelstepper question :)

sn

For more options, visit https://groups.google.com/d/optout.



--
Sandy Noble

Brinmetal

unread,
Sep 7, 2017, 1:51:01 AM9/7/17
to accelstepper
Hy Folks

I am very new in Arduino, and I dont understand lot of relations in programing. Of course I am not native English, and please apologize for grammatic mistakes.
My plan is to make a power press feeder with accel stepper. I download the sketch(basic) and its really work good with stepper motor. Up to now I used Mach3 for that job, but for the Mach 3 is needed a PCand Monitor, and its quite big.
What is the plan? 
The program needed to have LCD(16/2) with buttons to set the value(steps).(distance to go)
Also a start button for starting the motor each time.
The motor is need to move only in forward, cancel the value, and move again, an again.


Is that simple? For me its not.
Here is the sketch what I used:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 9, 8);

int pos = 3600;

void setup()
{  
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
}

void loop()
{
  if (stepper.distanceToGo() == 0)
  {
    delay(500);
    pos = -pos;
    stepper.moveTo(pos);\
  }
  stepper.run();
}

Thank you for helping.

Arpad
Message has been deleted

Brinmetal .

unread,
Sep 8, 2017, 1:58:41 AM9/8/17
to accels...@googlegroups.com
Thanks Gregor

Maybe it is a stupid question, but I dont see the how to set up a display, and what kind of display it is?
Also my other problem is how to change the stepping value with a buttons?
Maybe its a complicated project..........
On arduino forum someone suggest to me that is better to have 2 arduinos. One for stepping, other for setup the stepping value with lcd and buttons.
I tested one sketch and it is enough good for me for stepping. Only one problem left.
How to change stepping value and send thru serial from one uno to another.
Did you have some solition for that?

Here is my sketch:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 9, 8);

#define  BUTTON  10
#define  LEDPIN  13

int pos = 3600;

void setup()
{  
  stepper.setMaxSpeed(6000);
  stepper.setAcceleration(10000);
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(LEDPIN, OUTPUT);
}

void loop()
{
  if (stepper.distanceToGo() == 0 && digitalRead(BUTTON) ==LOW)
  {
    
    stepper.move(pos);
  }

  if (stepper.distanceToGo() == 0)
  {
    digitalWrite(LEDPIN, HIGH);
  }
  
   else
    {
      digitalWrite(LEDPIN, LOW);
    }
  stepper.run();

}

2017-09-07 9:43 GMT+02:00 gregor <chris...@gmail.com>:
hi,
you could try something like this (untested):
#include <AccelStepper.h>

#define PIN_PLUS 2
#define PIN_MINUS 3
#define PIN_START 4
#define DEBOUNCE_DELAY 20


// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 9, 8);

int steps_required = 1000;

uint32_t update_last
= 0L;

void updateDisplay()
{
   
//TODO implement
}

void setup()
{  
    pinMode
(PIN_PLUS, INPUT_PULLUP);
    pinMode
(PIN_MINUS, INPUT_PULLUP);
    pinMode
(PIN_START, INPUT_PULLUP);

    stepper
.setMaxSpeed(3000);
    stepper
.setAcceleration(1000);
}

void loop()
{
   
//update what is shown on the display
    updateDisplay
();
   
   
//check plus / minus pins every now and then
   
if (millis() - update_last > DEBOUNCE_DELAY)
   
{
       
if (!digitalRead(PIN_PLUS))
            steps_required
++;
       
if (!digitalRead(PIN_MINUS))
            steps_required
--;
           
       
//reset timer
        update_last
= millis();
   
}
   
   
//if the stepper is stopped and the start button is being pressed
   
if ((millis() - update_last > DEBOUNCE_DELAY) && (stepper.distanceToGo() == 0) && (stepper.speed() == 0.0f) && !digitalRead(PIN_START))
   
{
       
//set new target for stepper
        stepper
.move(steps_required);
   
}
   
    stepper
.run();
}
the DEBOUNCE_DELAY is necessary because of contact bounce.

--
You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/n-ZaWyo2mwI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

               Best Regards

     


      Arpad Brindza


    Szövegközi kép 2

          +36 30 5583780

            






gregor

unread,
Sep 10, 2017, 12:27:37 PM9/10/17
to accelstepper
Hi, 

you could use SoftwareSerial to have 2 Arduinos communicate with each other. I have never used it, tough. 
In my code example I have deliberately left out the display code because this is outside the scope of this forum. For HD47780 and compatible displays (virtually all 16x2 to 20x4 displays) check the LiquidCrystal library
It's not actually necessary to use 2 Arduinos, but it gets a little complicated if you try to do both with 1. When using AccelStepper you are expected to call the run functions as often as possible, so efficiently using AccelStepper and a display at the same time becomes complicated. If you're interested in my approach, take a look at the DisplayHD47780 class of my YAAADevice project.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages