Encapsulate StpDuoBuff class and use timer2 timer to timing the steps. It leaves the movement more precise and relieves processing in the main loop. On the other hand, functions that depend on timer2 (such as tone ()) no longer works correctly, as do the PWM of pins 9 and 10 on the arduino UNO/Nano.
Public declaration:
#define STP_TIMER2_TIME 64 // microseconds class StpDuoTimer2{ public: // Construction StpDuoTimer2(); // Initialization void begin(uint startSpeed, uint cruiseSpeed); void beginLf(int mPin_1, int mPin_2, int mPin_3, int mPin_4); void beginRg(int mPin_1, int mPin_2, int mPin_3, int mPin_4); void turboOn(); void turboOff(); void halfStepOn(); void halfStepOff(); void setBrakeCutPercent(byte brakeCutPercent); void setReportSteps(long steps); // Operation void run(); void moveSteps(byte mvType, long steps); void moveSteps(TMoveParam* mov); boolean addMoveSteps(byte mvType, long steps); boolean addMoveSteps(TMoveParam* mov); void moveOn(byte mvType); void goNow(); void clearMoves(); void stopNow(); void decelStop(); void motorOff(); // Monitoring boolean halfStep(); boolean isMoving(); byte getMoveType(); byte movesCount(); ulong overlaps(); protected: // Events, override to capture virtual void onStepsDone(long steps, long totalSteps){} virtual void onStartMoveSteps(byte mvType, long steps){} virtual void onMoveStepsDone(byte mvType, long steps){} virtual void onAllMovesDone(){} };
Read first the topics discussed on the Stepper Motors page.
Call begin() before any method to inform startSpeed and cruiseSpeed. Next, call beginLf() and beginRg() methods to inform control pins of stepper motors.
Then select the operation mode of motors (half step, turbo or none).
If needed, configure the brake cut percent calling setBrakeCutPercent().
If you want to receive events when certain number of steps are performed, call setReportSteps() informing the number of steps to trigger the event onStepsDone(). The value zero on steps parameter disable onStepsDone() event.
Dont forget to call run() method at least once per loop. Its necessary to trigger events.
To start a not buffered move, call moveSteps() or moveOn(). moveSteps() execute the number of steps informed and stop (after decelerate). moveOn() causes the movement to continue until some method is called to stop.
To stop the movement immediately call stopNow(). To stop with deceleration call decelStop().
To add moves on buffer list, call addMoveSteps(), to execute the moves list, call goNow(), moves will be executed in sequence. To clear buffer list call clearMoves(). This not interrupt the current move. movesCount() return the number of moves on buffer, current move is not included.
Overlaps occur when one timer event is triggered before the last event is done.
Be sure that return of overlaps() method is always zero. If not, probably there are too much process on onMoveStepsDone() event, try to reduce or optimize the code. If you cant, increase the time between events by modifying the define STP_TIMER2_TIME.
Wiki: StepperCouple
Wiki: StepperDuo
Wiki: StepperMotors
Wiki: StepperSingle
Wiki: StpDuoBuff