tyhenry / CheapStepper

Arduino library for the cheap but decent 28BYJ-48 5v stepper motor with ULN2003 board
GNU General Public License v3.0
123 stars 45 forks source link

newMoveTo does not work correctly with toStep less than current position #18

Open drp0 opened 5 years ago

drp0 commented 5 years ago

If toStep is less than stepN the move to is incorrect. This also affects newMoveToDegree.

My solution:

void CheapStepper::newMoveTo (bool clockwise, int toStep){

    // keep toStep in 0-(totalSteps-1) range
    if (toStep >= totalSteps) toStep %= totalSteps;
    else if (toStep < 0) {
        toStep %= totalSteps; // returns negative if toStep not multiple of totalSteps
        if (toStep < 0) toStep += totalSteps; // shift into 0-(totalSteps-1) range

    lastStepTime = micros();

    }

    if (clockwise) { // drp correction for moveto < current position
        if (toStep < stepN) toStep = toStep + totalSteps;
    stepsLeft = toStep - stepN;
    //stepsLeft = abs(toStep - stepN);
    }
    else {
        if (toStep < stepN) { // drp correction for moveto < current position

        stepsLeft= toStep - stepN;
        }else{
        //stepsLeft = -1*(totalSteps - abs(toStep - stepN));
        stepsLeft = -1*(totalSteps - (toStep - stepN));
        }
    }

lastStepTime = micros();
}

As a bonus, abs no longer needed

David