#include “DualVNH5019MotorShield.h”
DualVNH5019MotorShield md;
// The constant below will scale the peak output voltage to the motor by the percentage
// declared. For example, if you want the peak voltage to the motor to be equal to VIN,
// set percentOutput to 100. If you want the peak voltage to the motor to be 80% of VIN,
// set percentOutput to 80. You should see what voltage the stepper motor is rated for
// and set percentOutput appropriately. The value of percentOutput should be between 0
// and 100.
const byte percentOutput = 50;
#define QUARTER_STEP 1
#define HALF_STEP 2
#define FULL_STEP 4
unsigned char stepMode = FULL_STEP;
// This function will set the voltage applied to each coil.
inline void set_speeds(int m1speed, int m2speed)
{
md.setSpeeds(m1speed/2percentOutput/50, m2speed/2percentOutput/50);
}
// 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%)
set_speeds(283, 283);
break;
case 1: // quarter step (coil 1 at 38% and coil 2 at 93%)
set_speeds(153, 370);
break;
case 2: // half step (coil 1 at 0% and coil 2 at 100%)
set_speeds(0, 400);
break;
case 3: // quarter step
set_speeds(-153, 370);
break;
case 4: // full step
set_speeds(-283, 283);
break;
case 5: // quarter step
set_speeds(-370, 153);
break;
case 6: // half step
set_speeds(-400, 0);
break;
case 7: // quarter step
set_speeds(-370, -153);
break;
case 8: // full step
set_speeds(-283, -283);
break;
case 9: // quarter step
set_speeds(-153, -370);
break;
case 10: // half step
set_speeds(0, -400);
break;
case 11: // quarter step
set_speeds(153, -370);
break;
case 12: // full step
set_speeds(283, -283);
break;
case 13: // quarter step
set_speeds(370, -153);
break;
case 14: // half step
set_speeds(400, 0);
break;
case 15: // quarter step
set_speeds(370, 153);
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 multi_step(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()
{
multi_step(400, 5000);
delay(1000);
multi_step(-400, 5000);
}