This page is going to add a simple function to our program that allows the stepper motor to be controlled positionally.
Notice how the 'struct' called motor has a value called stepsleft that we initialised as 0 but havent used so far. We want to make a function that allows us to move the motor a predefined number of steps.
The motors used have a 1.5 degree stepping angle this makes 240 steps for each full revolution (360degrees / 1.5). so when we make the function, a good test will be to tell it to step 200 times and see if it makes a full circle.
The motors that i am using come from sparkfun and are supposed to have a step angle of 1.8 degrees meaning that I should only need 200 steps per revolution. Trying this I was consistantly 1/6 of a full circle out, so keeping the coils energised meant that I could maually rotate it and count how many steps i was out. This turned out to be 40 steps and 1.5 x 240 = 360 so I think the datasheet must be the wrong one (the motor is a generic case style with no markings so its difficult to tell). Its important to know the correct amount of steps or you are going to build up massive error along the way.
For this example, speed ramping isnt used. It just moves the motor at a slow speed.
Here is the new program:
The program has changed a bit, Weve added the ability for the motor to go forwards or backwards.
If we call moveSteps(240); the wheel should do a full revolution forwards
if we call moveSteps(-240); the wheel should do a full revolution backwards
Lets test the forward movement
Now im happy with this part of the program, its time to add speed ramping. The next page describes a trapezoidal ramping technique