Stepper motor only rotate one way


I test motor with this code
Well motor rotate in clock wise direction. How do I control it so it can go back words too.

Motor is 4 wire bipolar.

Thanks for your time.



from bbio import *

pin1 = GPIO1_6
pin2 = GPIO1_7
pin3 = GPIO1_2
pin4 = GPIO1_3

pinMode(pin1, OUTPUT)
pinMode(pin2, OUTPUT)
pinMode(pin3, OUTPUT)
pinMode(pin4, OUTPUT)

Create a setup function:

def setup():

Set the two LEDs as outputs:

digitalWrite(pin1, LOW)
digitalWrite(pin2, HIGH)
digitalWrite(pin3, LOW)
digitalWrite(pin4, HIGH)

def loop():

Start the loop:



A stepper gets controlled by cycling through eight states. Here’s the code I use to drive a stepper motor (extract of example from libpruio)

//! Set values of all four output pins.
#define PIN_OUT(a, b, c, d)
if (pruio_gpio_setValue(Io, P1, a)) {printf(“setValue P1 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P2, b)) {printf(“setValue P2 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P3, c)) {printf(“setValue P3 error (%s)\n”, Io->Errr); break;}
if (pruio_gpio_setValue(Io, P4, d)) {printf(“setValue P4 error (%s)\n”, Io->Errr); break;}

/*! \brief Make the motor move the next step.
\param Io Pointer to PruIo structure.
\param Rot Rotation direction (1 or -1).

This function sets 4 output pins for a stepper motor driver. It
remembers the last step as static variable (starting at 0 = zero) and
adds the new position to it. So the Rot parameter should either be 1 or
-1 to make the motor move one step in any direction.

move(pruIo *Io, int Rot) {
static int p = 0;

p += Rot;
p &= Rot & 1 ? 7 : 6 ;

switch §{
case 1: PIN_OUT(1,0,0,0); break;
case 2: PIN_OUT(1,1,0,0); break;
case 3: PIN_OUT(0,1,0,0); break;
case 4: PIN_OUT(0,1,1,0); break;
case 5: PIN_OUT(0,0,1,0); break;
case 6: PIN_OUT(0,0,1,1); break;
case 7: PIN_OUT(0,0,0,1); break;
default: PIN_OUT(1,0,0,1)