AccelStepper Arduino Library

repository·master·Indexed 42 days ago

https://github.com/waspinator/accelstepper

An advanced Arduino library for controlling 2, 3, or 4-pin stepper motors. It provides support for acceleration, deceleration, and non-blocking multi-motor operation. Includes the MultiStepper class for coordinating multiple steppers to arrive at target positions simultaneously at constant speeds.

Tokens
936
Snippets
4
Records
6
Agent score
3%

What's inside AccelStepper

  1. Overview of the AccelStepper library

    master

    AccelStepper is an object-oriented Arduino library designed for controlling 2, 3, or 4-pin stepper motors and motor drivers. It is a more advanced alternative to the standard Arduino Stepper library, specifically designed for applications requiring complex motion profiles or multiple motors.

    Key capabilities include:

    • Acceleration and Deceleration: Smoothly ramps motor speed up and down.
    • Non-blocking API: Functions never call delay(), allowing for concurrent execution of other code.
    • Multi-motor Support: Enables independent, simultaneous stepping for multiple steppers.
    • Hardware Versatility: Supports 2, 3, and 4-wire steppers, including 3 and 4-wire half-stepping, and drivers like the Sparkfun EasyDriver (3967 chip).
    • Compatibility: Includes support for AFMotor (Adafruit Motor Shield) stepping functions.
  2. Coordinate multiple steppers with MultiStepper

    master

    The MultiStepper class manages multiple AccelStepper instances (up to a maximum of 10, defined by MULTISTEPPER_MAX_STEPPERS) to ensure they all arrive at their target positions at the same time. This is ideal for achieving linear motion in multi-axis systems like X-Y plotters or 3D printers.

    Important Limitations:

    • Constant Speed Only: MultiStepper does not support acceleration or deceleration. All managed steppers will move at a constant speed calculated to synchronize their arrival times.
    • Array Length: When using moveTo(), the provided array must be at least as long as the number of steppers added via addStepper() to avoid undefined behavior.
  3. Set target positions with moveTo()

    master

    The moveTo(long absolute[]) method sets the target absolute positions for all managed steppers. The class automatically computes new constant speeds for each stepper so they all arrive at their respective targets at approximately the same time.

    Parameters:

    • absolute[]: An array of desired absolute positions. absolute[0] corresponds to the first stepper added via addStepper(), absolute[1] to the second, and so on.
    long positions[2] = {1000, 500};
    multi.moveTo(positions);
  4. Add an AccelStepper to MultiStepper

    master

    Use addStepper(AccelStepper& stepper) to include a stepper in the managed set. The class supports a maximum of MULTISTEPPER_MAX_STEPPERS (10) steppers.

    Returns:

    • true if the stepper was successfully added.
    • false if adding the stepper would exceed the maximum limit.
    MultiStepper multi;
    // ... setup stepper ...
    if (!multi.addStepper(stepper)) {
        // Handle error: too many steppers added
    }
  5. Run MultiStepper motion non-blockingly with run()

    master

    The run() method calls runSpeed() on all managed steppers that have not yet reached their target position. This should be called frequently in your main loop to allow motion to progress.

    Returns:

    • true if any stepper is still moving toward its target.
    • false if all steppers have reached their targets.
    // In your main loop
    if (multi.run()) {
        // Steppers are still moving
    } else {
        // All steppers have arrived
    }
  6. Run MultiStepper motion blockingly with runSpeedToPosition()

    master

    The runSpeedToPosition() method blocks execution of your program until all managed steppers have reached their target positions. Use this if you do not need to perform other tasks while the motors are moving.

    long positions[2] = {1000, 500};
    multi.moveTo(positions);
    multi.runSpeedToPosition(); // Execution stops here until motion is complete