MTulnStepper
Event management for Uno, Nano, Mega

For a stepper motor, if the pilot is a ULN2003 (28BYJ48 stepper) or a unipolar stepper whose 4 coils are commanded directly, use the MTulnStepper object. Otherwise, see on the side of MTstepStepper.

Include to add

#include <MTobjects.h> // See http://arduino.dansetrad.fr/en/MTobjects

I strongly advise to leave the comment, if another person wanted to try the program, they would have a download link directly. This is valid for all the libraries you use.

Useful constants

RPM
RPS
UNCONNECTED
HALF_STEP
WHOLE_STEP
ONE_PHASE
DEFAULT_SENSE
REVERSE
CONTINUE
NO_ACTION
NO_ACCELERATION

RPM indicates a r/min constant (rotate per minute)
RPS indicates a r/s constant (rotate per second)
UNCONNECTED for an unconnected A2 and B2 pin
HALF_STEP, WHOLE_STEP, ONE_PHASE to choose the mode
DEFAULT_SENSE, REVERSE to reverse or not the sens
CONTINUE allows continuous rotation for move, moveTo
NO_ACTION default parameter if you do not call for functions at the end of the rotation

Useful comments

step_or_micro_step
micro_seconds
micro_second_per_step
absolute_position
accelerations_of
pin_A1
pin_B1
pin_A2
pin_B2

These comments are words that can be added anywhere and which are ignored by the compiler.

Constructor

MTulnStepper(pinA1, pinB1,
             pinA2 = UNCONNECTED,
             pinB2 = UNCONNECTED,
             acceleration = 2048 step_or_micro_step,
             mode = WHOLE_STEP,
             speed = 0.2 RPS,
             reverse= DEFAULT_SENSE,
             onStopFunction= NO_ACTION,
             stepsPerTurn = 2048);

Useful functions

void setSpeed(float speed);
void move(int32_t nbSteps);
void moveAndWait(int32_t nbSteps);
void stop(void);
void wait(void);
bool stopped(void);
void setPosition(int_t position);
int32_t getPosition(void);
void setOrigin(void);
void disable(void);
void enable(void);
int32_t getRest(void);
void onStop(void);

setSpeed(<digital value> micro_second_per_step): program a rotation speed
setSpeed(<digital value> RPS): program a rotation speed
setSpeed(<digital value> RPM): program a rotation speed
move: go forward (if nbSteps>0) or backward (if nbSteps <0) of nbSteps steps. By default the numbers are on 32 bits, but they can be passed in 16 bits, see MTobjects.h. Function is not blocking, we immediately go out, the stepper will continue its race without anyone taking care of it
move(CONTINUE): advance constantly (you can stop it by stop(); !)
move(-CONTINUE): go back constantly
moveAndWait: advance or decrease but the function awaits the stop
moveAndWait(CONTINUE): advance or decrease (with -CONTINUE) but the function awaits the stop, in this case only by stop();
stop: stop the stepper
wait: blocking function as long as the stepper runs
stopped: return true if the engine is stopped
setPosition: arbitrarily fixes a position
getPosition: return the engine position
setOrigin: fixes the original position here (for example after a machine originating)
disable: disable the driver outputs
enable: réactive les sorties du driver
getRest: reactivate the driver outputs
onStop: function to be overloaded called when the stepper stops and at initialization

Bonus functions

void setReverse(boolean);
void setOnStopFunction(onStopFunction);
void setStepsPerTurn(word stepsPerTurn);
word getStepsPerTurn(void);
float getSpeedMicrosecond(void);
float getSpeedRPS(void);
float getSpeedRPM(void);
void setHalfStep(char mode);
void setAcceleration(int_t acceleration);

setReverse(REVERSE): allows to reverse the sens of rotation
setReverse(DEFAULT_SENSE:) allows not to reverse the sens of rotation
setOnStopFunction: change the call back function when stopping
setOnStopFunction: without parameters removes the call back function when stopping
setStepsPerTurn: fixes the number of steps per turn
setStepsPerTurn: without parameters fixes the number of steps per turn at 2048
getStepsPerTurn: return the number of steps per turn
getSpeedMicrosecond: return time between 2 steps in µs
getSpeedRPS: return the speed in RPS
getSpeedRPM: return the speed in RPM
setMode(WHOLE_STEP): go to whole step mode, two phases at a time. Use after setStepsPerTurn()
setMode(ONE_PHASE): go to whole step mode, one phase at a time. Use after setStepsPerTurn()
setMode(HALF_STEP): go to half step mode. Use after setStepsPerTurn()
setAcceleration: changes the number of steps for acceleration

Examples