@@ -111,7 +111,7 @@ Stepper::Stepper(uint8_t enablePin_, uint8_t stepPin_, uint8_t directionPin_, ui
111111
112112void Stepper::doReset () {
113113 state = S_READY ;
114-
114+ forceStop = false ;
115115 position = 0 ;
116116
117117 digitalWrite(directionPin, LOW );
@@ -185,7 +185,7 @@ boolean Stepper::moveRelative(long steps, long& time) {
185185}
186186
187187boolean Stepper::home () {
188- if ( busy() ) {
188+ if ( busy() ) {
189189 return false ;
190190 }
191191
@@ -208,6 +208,14 @@ boolean Stepper::home() {
208208 return canMove;
209209}
210210
211+ void Stepper::stop () {
212+ if ( ! busy() ) {
213+ return ;
214+ }
215+
216+ forceStop = true ;
217+ }
218+
211219/*
212220 function line(x0, x1, y0, y1)
213221 int deltax := x1 - x0
@@ -232,9 +240,14 @@ void Stepper::doInterrupt() {
232240 }
233241
234242 bool doneMoving = false ;
235-
243+
244+ if ( forceStop ) {
245+ state = S_USER_STOP ;
246+ doneMoving = true ;
247+ forceStop = false ;
248+ }
236249 // Check if we just walked into the limit switch
237- if ( settings. canHome && digitalRead(limitPin) == LOW ) {
250+ else if ( settings. canHome && digitalRead(limitPin) == LOW ) {
238251 if ( ( direction < 0 && settings. homeDirection == H_BACKWARD ) ||
239252 ( direction > 0 && settings. homeDirection == H_FORWARD ) )
240253 {
@@ -296,8 +309,8 @@ void Stepper::doInterrupt() {
296309 }
297310 }
298311
299- // Stopping motion tasks
300- if (doneMoving) {
312+ // Stopping motion tasks (doneMoving == TRUE)
313+ else {
301314 if (settings. stopMode = S_DISABLE ) {
302315 digitalWrite(enablePin, HIGH );
303316 }
@@ -377,6 +390,10 @@ boolean Stepper::checkFinished() {
377390 state = S_READY ;
378391 return true ;
379392 }
393+ else if (state == S_USER_STOP ) {
394+ state = S_READY ;
395+ return true ;
396+ }
380397
381398 return false ;
382399}
0 commit comments