28BYJ-48 Stepper motor code

Will update it here when I get feedback or feature requests:

Here's some help to get you started:


Here is a brief summary of the code.
It can spin a full size DSLR on it's shaft without bearings at 30RPM!
Ramping up and slowing down to prevent bouncing.
It can measure torque to detect an abnormal load or error.
It works without blocking or waiting or interrupts.
Enhanced performance using 12v without warming up the motor.
And much more!
Sounds like an advert, but it's all for Free.
Hardware is $4.

bcw 1 is clockwise 0 is ccw
deg100 9000 is 90 deg
rpm100 1000 is 10 rpm, 100 is sec hand
rph100 hour 100 is like minute hand on clock
percslow 10 is 10% slow at end of move
steppos 90.0*STEPERDEG is 90 deg
steps same
xinrev 0,2,3,4,8x in 360 deg
revo # revolutions
const float STEPERDEG=float(64)*64/360;

These are ordered just like in the code:
void unramp (boolean bcw, long deg100, int rpm100, int percslow)
void unramp1 faster
void moveto (int steppos)
void movetodir (boolean bcw, int steppos)

void midspeedcool_12v (boolean bcw, long steps)
void ramp (boolean bcw, int rpm100)
void revRestart (boolean bcw, long revo, int rpm100, int xinrev)
void rev (boolean bcw, long revo)

void degrpmslowCool4 (boolean bcw, long deg100, int rph100)
void degrpmslowCool4_nowait
int calloften()
void degrpm_nowait (boolean bcw, long deg100, int rpm100)
int calloften_micro (boolean bcw)

void degrpmslowCool (boolean bcw, long deg100, int rph100)
void degrpmslowHot
void degrpmslow2

void degrpmEZ (boolean bcw, long deg100, int rpm100)
void degrpm
void degrpm8
void move (boolean bcw)

1 line description for each function above:
unramp stops bounce at end of move
unramp1 faster
absolute position in degrees shortest direction
absolute choose direction

12v only 1/2 speed
speed up over 90 deg
restarts x in 360 rev

most efficient mAh
slow no wait
fast no wait
look at d in loop

cool 4 step
hot 4 step
mid 4 step

8 step 5 lines simple fast ramp, measure torque
8 step 5 lines +err 2.00deg move delay(10)
microstep speed limit 17RPM
relative move 1 step

ccwss() measures torque and halts program if spike to prevent damage
just hook motor winding to analog pin 0

Non-blocking. No delay(). More than one motor. Run other tasks at the same time in Loop()

The library is setup to run other code in loop while spinning the motor non-blocking without using interrupts. First initialize the move you want to make 1x. This could be done in setup or an if statement in loop for example:

void degrpm_nowait (boolean bcw, long deg100, int rpm100)

Then call this function often:

int calloften_micro (boolean bcw)

At faster speeds you have to call it more often or it will get behind and run slower. You can look at d in loop to see how much time you have in micros. There are lines to uncomment if you want it to be more smooth consistent and partly blocking, some of the time. The calloften functions use little integer math so they are fast.

There is another pair of functions for going slower and cooler using 12v:
void degrpmslowCool4_nowait (boolean bcw, long deg100, int rph100)
int calloften()

With these functions you have much more free time to process in loop, d milliseconds. If you miss a step it's no big deal, the motor will catch up to keep the speed mostly constant. It will move the correct distance no matter what you do wrong with timing.

All you have to do is choose the RPM and *100.

You do not have to read the torque pin yourself unless you want to. My function ccwss() measures torque and halts program if spike to prevent damage. Look in that function for example code to see how you could read it to print a warning instead of halting. But you do have to wire it up. Just hook any motor winding to analog pin 0. If you're worried about damage use a resistor like 1k. Pin 0 does not give you any absolute data, only relative. After it has been running the code stores the normal value at the normal load. Anything abnormal will cause the program to halt. It detects increased torque as well as zero torque if a linkage were to break. The current the motor consumes is a constant amperage with a given voltage supply. You can measure this with an ammeter and store it as a constant as it will not change much, even when not moving. Use the function off() to save power and keep the motor cool when not moving.