8 responses to “Arduino 28BYJ-48 stepperbot”

  1. David Palmer

    Your sketch can be a lot simpler by using helper functions and lookup tables.
    Here are some suggestions to get you started.
    (None of this has been tested for syntax or function, but if you can understand
    what I’m suggesting, you will see how to shrink your code.)

    // What are the current positions of the motors
    // Start them all at position zero
    int8_t currentSteps[4] = [0,0,0,0];

    // What pattern to use for each step phase
    const uint8_t steptable[8][4] = [
    [HIGH,LOW,LOW,LOW],[HIGH,HIGH,LOW,LOW],
    [LOW,HIGH,LOW,LOW],[LOW,HIGH,HIGH,LOW],
    [LOW,LOW,HIGH,LOW],[LOW,LOW,HIGH,HIGH],
    [LOW,LOW,LOW,HIGH],[HIGH,LOW,LOW,HIGH]];

    const int8_t speedForward[4] = [1,1,1,1];
    const int8_t speedBackward[4] = [-1,-1,-1,-1];

    // motor 1 = backward / motor 2 = forward / motor 3 = forward / motor 4 = backward
    // From your comment for sideleft. By the way, a software person
    // would number the motors starting from 0
    const int8_t speedSideLeft[4] = [-1,1,1,-1];
    //etc.

    // Motor is 0,1,2,3 .
    // Step can be any number, but then it gets divided down by 8 and the
    // remainder is the step that’s used
    int StepMotor(int motor, int step)
    {
    step = step % 8; // The % operator takes the remainder when divided by 8
    if (step < 0) step += 8; // The remainder can be negative (-7 to -1), so fix that
    for (int wire = 0; wire < 4 ; wire++)
    {
    setRegisterPin(wire + 4*motor, steptable[step][wire]);
    }
    return step;
    }

    void StepBySpeed(const int8_t speed[4])
    {
    for (int motor = 0 ; motor = previousMicros)
    {
    previousMicros = previousMicros+stepInterval;
    StepBySpeed(speedForward);
    }
    }

    // etc.

  2. David Palmer

    // Some of the code got mangled. Starting with StepBySpeed:

    void StepBySpeed(const int8_t speed[4])
    {
    for (int motor = 0 ; motor = previousMicros)
    {
    previousMicros = previousMicros+stepInterval;
    StepBySpeed(speedForward);
    }
    }

    // etc.

  3. David Palmer

    WordPress keeps corrupting the code. I sent you the code by email instead.

  4. Minde

    To publish code use Sytanx Highlighter plugin. You can then publish nice code with all indentations and proper formating.

  5. me

    where did you get the wheels, and how did you fix them to the motor?

Leave a Reply

*