Continuing with my stroll through the
Adafruit Learning System, I set up a stepper motor with a ULN2803 according to
lesson 10 and wrote some
Mathematica code WolframLanguage code to control the stepper motor. Below is a small package that does the trick.
BeginPackage["gpiomotor`"]
stepMotor::usage = "stepMotor[steps, direction, delay] where steps is an integer from 1 to 128, direction is 1 for forward and 0 for backward and delay is step time in ms (values less than 5 aren't meaningful).";
motorRelease::usage = "motorRelease[] releases the coils so the motor does not draw current";
$coila1pin = 4;
$coila2pin = 17;
$coilb1pin = 23;
$coilb2pin = 24;
Begin["`Private`"]
$coils = {$coila1pin, $coila2pin, $coilb1pin, $coilb2pin};
$steps = {{1,0,1,0},{0,1,1,0},{0,1,0,1},{1,0,0,1}};
Clear[stepMotor]
stepMotor[steps_, direction_, delay_:5]:=Module[{dms,i},
dms = delay/1000;
For[i = 0, i < steps, i++,
Map[(setStep[#];Pause[dms];)&,If[direction == 0, Reverse@$steps, $steps]]
]
]
Clear[motorRelease]
motorRelease[]:=Module[{},
setStep[{0,0,0,0}]
]
Clear[setStep]
setStep[array_]:=Module[{},
MapThread[DeviceWrite["GPIO",#1->#2];&,{$coils,array}];
]
End[]
EndPackage[]
Using Mathematica's `Map` function makes the code a little bit tighter than the corresponding tutorial python code. As with my other discussion about `DeviceWrite`, it's quite slow, and you cannot currently set the delay to a small enough value to make the motor move smoothly. However, using c/MathLink or `BinaryWrite` can improve the hardware interface access time much like other discussions presented in this forum. I wanted to present a pure Mathematica solution here.