Hello,
Below is a sample code to run a stepper motor with the MC33926 Motor Driver Shield (pololu.com/catalog/product/2503). Additionally this code shows how you can adjust the size of the step between full step, half step and quarter step. See the comments in the code for more detail.
#include "DualMC33926MotorShield.h"
DualMC33926MotorShield md;
#define QUARTER_STEP 1
#define HALF_STEP 2
#define FULL_STEP 4
unsigned char stepMode = FULL_STEP;
// Advances the stepper motor by one step either clockwise or counterclockwise
// with the direction specified by the argument dir (0 or 1). The size of the
// step depends on stepMode and can either be a full step, a half step, or a
// quarter step. Full stepping is produced by repeating a four-state cycle
// in which both coils are always energized to carry the same magnitude of current
// but the direction of the current is sequentially switched. Running through the
// four-state cycle in the reverse order reverses the direction of rotation. The
// general equation for coil current should be as follows:
// coil 1 current = I * sin(a)
// coil 2 current = I * cos(a)
// When full stepping, the four states are:
// forwards: a = 0, 90, 180, 270 degrees
// reverse: a = 0, 270, 180, 90 degrees
// half stepping comes from: a = 0, 45, 90, 135, 180, 225, 270, 315 degrees
// quarter stepping comes from a = the 16 multiples of 22.5 from 22.5 to 360 deg
void one_step(unsigned char dir)
{
// this static variable lets us remember what step we're on so we
// can change to the appropriate next state in the sequence
static unsigned char step = 0;
// compute the next step based on the direction argument dir
// and the step mode. Full stepping skips half and quarter steps,
// and half stepping skips quarter steps. Quarter stepping cycles
// through all 16 steps.
if (dir == 1)
step += stepMode;
else
step -= stepMode;
switch (step & 15)
{
case 0: // full step (both coils energized at 71%)
md.setSpeeds(180, 180);
break;
case 1: // quarter step (coil 1 at 38% and coil 2 at 92%)
md.setSpeeds(98, 236);
break;
case 2: // half step (coil 1 at 0% and coil 2 at 100%)
md.setSpeeds(0, 255);
break;
case 3: // quarter step
md.setSpeeds(-98, 236);
break;
case 4: // full step
md.setSpeeds(-180, 180);
break;
case 5: // quarter step
md.setSpeeds(-236, 98);
break;
case 6: // half step
md.setSpeeds(-255, 0);
break;
case 7: // quarter step
md.setSpeeds(-236, -98);
break;
case 8: // full step
md.setSpeeds(-180, -180);
break;
case 9: // quarter step
md.setSpeeds(-98, -236);
break;
case 10: // half step
md.setSpeeds(0, -255);
break;
case 11: // quarter step
md.setSpeeds(98, -236);
break;
case 12: // full step
md.setSpeeds(180, -180);
break;
case 13: // quarter step
md.setSpeeds(236, -98);
break;
case 14: // half step
md.setSpeeds(255, 0);
break;
case 15: // quarter step
md.setSpeeds(236, 98);
break;
}
}
// This is a blocking function that repeatedly takes a single step and then
// delays for step_delay_us microseconds. When it finishes, the stepper motor
// coils will continued to be energized according to the final step so that
// the stepper motor maintains its position and holding torque.
void multistep(int steps, unsigned int step_delay_us)
{
unsigned char dir = 1;
if (steps < 0)
{
dir = 0;
steps = -steps;
}
while (steps--)
{
one_step(dir);
delayMicroseconds(step_delay_us);
}
}
void setup()
{
md.init();
}
void loop()
{
if (stepMode == FULL_STEP)
stepMode = HALF_STEP;
else
stepMode = FULL_STEP;
multistep(400, 5000);
delayMicroseconds(500);
unsigned char i;
for (i = 0; i < 4; i++)
{
multistep(-100, 2000);
delayMicroseconds(100);
}
}
This code was adapted from the stepper-motor1 example for the Orangutan, which can be found at this link: github.com/pololu/libpololu-avr … or1/test.c
- Takuya Jeremy