Efficient code for stepper motor control?

I would like to run a couple of steppers but can't afford for the system to be hung during the entire move time. The stepper motor lib says it it blocking code.

Any ideas besides extra hardware?

Hi there,

What hardware are you planning to use, eg H-Bridge, Darlington array or just a bunch of FETs?

It would be possible with a bit of inginuity to use the arduino timers to run the motor a preset number of steps in either direction or run the motor at a given speed. This would mean that you could free up some processing time to do other things.

I have recenlty been playing with driving a unipolar stepper from a shift register and some FETs. It is not ideal as you can only manage full or half stepping but it is really easy; you just set up an array of bytes with the bits set to represent the coil you want to be switched on and loop through it with a pointer.

This is the code I used for that: (sorry, but it is VERY rough)

int seq[4] ={B00000001, B00000010, B00000100, B00001000}; //full step (wave drive) sequence
int hseq[8] ={B00000001, B00000011, B00000010, B00000110, //half step
             B00000100, B00001100, B00001000, B00001001};
int prt = 0; //pointer
long dly = 0; //delay
long prevdly = 10000; //previous delay (used for accelleration profile)
int accel = 8; //max accelleration
int dr = 1; //direction
int cnt = 0; //counter

void setup()
  pinMode(5, OUTPUT); //pins for sending data to the shift register
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

void loop()
  dly = analogRead(0)*5 + 200; //read the delay from a pot
  if (dly > prevdly) {
    dly = prevdly + accel;
  } else if (dly < prevdly) {
    dly = prevdly - accel;
  stp(); //step the motor
  delayMicroseconds(dly); //wait.....
  prevdly = dly; 

void stp()
  digitalWrite(6, HIGH);
  shiftOut(5,7,MSBFIRST,hseq[prt]); //shift the data out
  digitalWrite(6, LOW);
  prt  = prt + dr; //increment the pointer
  if (prt > 7) {
    prt = 0;
  } else if (prt < 0) {
    prt = 7;

Now, as you stated in your post, you can’t afford the system to be hung the whole time. To get round this is would be possible to use the arduino timers along with their associated interrupts to run the motors in the background. The timers seem a bit daunting when you first look into them, but if you can give me some more information on exactly what it is you are trying to do then I will do my best to help.


Thanks for input. I have yet to aquire hardware. Probably use an easydriver. I took the "blink without delay" example and modified to output a fixed number of pulses at a fixed rate. Looks like this will work. 8-)

I’m going to risk showing my ignorance here.
If I understand the code right then you put a serial bit stream out on pin 5.
Does this then have to go to a decoder to pulse the right coils? Could you not do the decoding on board and have four pins associated with the windings (through a driver) would this not make the chip count lower and the project easier? Secondly I’m confused how the acceleration bit works. If the dly is a delay then by making it bigger i.e. Prevdly + accel would that not make the motor run slower? I’m very new to this and trying to understand the ins and outs so please don’t take this as a knock on your project.