Driving two stepper motors with usb keyboard

I'm writing a code to use a wireless keyboard to control two stepper motors. Everything is basically ready except when I press a button the stepper motors only make one step. I was wondering if anyone can tell how to repeat stepping while the button is pressed.

The code:

/* Lokaverkefni */

/* Kóðabankar innfelldir */
#include <Spi.h> //Kóðabanki fyrir Serial Peripheral Interface
#include <Max3421e.h> //Kóðabanki fyrir kubbinn á USB Host Shield
#include <Usb.h> //Kóðabanki fyrir Universal Serial Bus

/* Útgangspinnar skrefmótora skilgreindir */

int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int motorPin5 = 14;
int motorPin6 = 15;
int motorPin7 = 16;
int motorPin8 = 17;

#define buttonUp (0x52)
#define buttonDown (0x51)
#define buttonLeft (0x50)
#define buttonRight (0x4f)
#define noButton (0x00)
 
/* keyboard data taken from configuration descriptor */
#define KBD_ADDR  1
#define KBD_EP    1
#define EP_MAXPKTSIZE  8
#define EP_POLL        0x0a
/**/
EP_RECORD ep_record[ 2 ];  //endpoint record structure for the keyboard
 
void setup();
void loop();
 
MAX3421E Max;
USB Usb;
 
void setup()
{
    Serial.begin( 9600 );
    Serial.println("Start");
    Max.powerOn();
    delay( 200 );
    
    pinMode( motorPin1, OUTPUT);
    pinMode( motorPin2, OUTPUT);
    pinMode( motorPin3, OUTPUT);
    pinMode( motorPin4, OUTPUT);
    pinMode( motorPin5, OUTPUT);
    pinMode( motorPin6, OUTPUT);
    pinMode( motorPin7, OUTPUT);
    pinMode( motorPin8, OUTPUT);
}
 
void loop()
{
    Max.Task();
    Usb.Task();
    if( Usb.getUsbTaskState() == USB_STATE_CONFIGURING ) {  //wait for addressing state
        kbd_init();
        Usb.setUsbTaskState( USB_STATE_RUNNING );
    }
    if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {  //poll the keyboard
        kbd_poll();
    }
}
/* Initialize keyboard */
void kbd_init( void )
{
  byte rcode = 0;  //return code
/**/
    /* Initialize data structures */
    ep_record[ 0 ] = *( Usb.getDevTableEntry( 0,0 ));  //copy endpoint 0 parameters
    ep_record[ 1 ].MaxPktSize = EP_MAXPKTSIZE;
    ep_record[ 1 ].Interval  = EP_POLL;
    ep_record[ 1 ].sndToggle = bmSNDTOG0;
    ep_record[ 1 ].rcvToggle = bmRCVTOG0;
    Usb.setDevTableEntry( 1, ep_record );              //plug kbd.endpoint parameters to devtable
    /* Configure device */
    rcode = Usb.setConf( KBD_ADDR, 0, 1 );
    if( rcode ) {
        Serial.print("Error attempting to configure keyboard. Return code :");
        Serial.println( rcode, HEX );
        while(1);  //stop
    }
    /* Set boot protocol */
    rcode = Usb.setProto( KBD_ADDR, 0, 0, 0 );
    if( rcode ) {
        Serial.print("Error attempting to configure boot protocol. Return code :");
        Serial.println( rcode, HEX );
        while( 1 );  //stop
    }
    
    delay(2000);
    Serial.println("Lyklabord virkt");
}
/* Poll keyboard and print result */
void kbd_poll( void )
{
  char i, j;
  char buf[ 8 ] = { 0 };      //keyboard buffer
  static char old_buf[ 8 ] = { 0 };  //last poll
  byte rcode = 0;     //return code
    /* poll keyboard */
    rcode = Usb.inTransfer( KBD_ADDR, KBD_EP, 8, buf );
    if( rcode != 0 ) {
        return;
    }//if ( rcode..
    for( i = 0; i < 8; i++ ) {
        if( buf[ i ] != old_buf[ i ] ) {
            for( j = 0; j < 8; j++ ) {
            switch( buf[ j ] ) {
            case buttonUp:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);        
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
         break;
            case buttonDown:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);        
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
         break;
            case buttonLeft:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);        
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
         break;
            case buttonRight:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);        
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
         break;         
        case noButton:
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          break;
            }                       
                old_buf[ j ] = buf[ j ];
            }//for( j = ...
        }//if( buf...        
    }// for( i = 0...    
}

I'm using duemilanove and sparkfun usb host shield btw

max.task() and usb.task() are missing from your code. Posting them may help someone better analyze your code.

you mean this?

Max.Task(); Usb.Task(); if( Usb.getUsbTaskState() == USB_STATE_CONFIGURING ) { //wait for addressing state kbd_init(); Usb.setUsbTaskState( USB_STATE_RUNNING ); }

it's in there.

I got it working but the motors slowed way down and the delay has no effect on speed anymore. Is it the do check that is slowing them down?

Edit: Delay() has effect, but there's an unwanted delay fixed between repeats

code below:

/* Lokaverkefni */

/* Kóðabankar innfelldir */
#include <Spi.h> //Kóðabanki fyrir Serial Peripheral Interface
#include <Max3421e.h> //Kóðabanki fyrir kubbinn á USB Host Shield
#include <Usb.h> //Kóðabanki fyrir Universal Serial Bus

/* Útgangspinnar skrefmótora skilgreindir */

int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int motorPin5 = 14;
int motorPin6 = 15;
int motorPin7 = 16;
int motorPin8 = 17;

#define buttonUp (0x52)
#define buttonDown (0x51)
#define buttonLeft (0x50)
#define buttonRight (0x4f)
#define noButton (0x00)
 
/* keyboard data taken from configuration descriptor */
#define KBD_ADDR  1
#define KBD_EP    1
#define EP_MAXPKTSIZE  8
#define EP_POLL        0x0a
/**/
EP_RECORD ep_record[ 2 ];  //endpoint record structure for the keyboard
 
void setup();
void loop();
 
MAX3421E Max;
USB Usb;
 
void setup()
{
    Serial.begin( 9600 );
    Serial.println("Start");
    Max.powerOn();
    delay( 200 );
    
    pinMode( motorPin1, OUTPUT);
    pinMode( motorPin2, OUTPUT);
    pinMode( motorPin3, OUTPUT);
    pinMode( motorPin4, OUTPUT);
    pinMode( motorPin5, OUTPUT);
    pinMode( motorPin6, OUTPUT);
    pinMode( motorPin7, OUTPUT);
    pinMode( motorPin8, OUTPUT);
}
 
void loop()
{
    Max.Task();
    Usb.Task();
    if( Usb.getUsbTaskState() == USB_STATE_CONFIGURING ) {  //wait for addressing state
        kbd_init();
        Usb.setUsbTaskState( USB_STATE_RUNNING );
    }
    if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {  //poll the keyboard
        kbd_poll();
    }
}
/* Initialize keyboard */
void kbd_init( void )
{
  byte rcode = 0;  //return code
/**/
    /* Initialize data structures */
    ep_record[ 0 ] = *( Usb.getDevTableEntry( 0,0 ));  //copy endpoint 0 parameters
    ep_record[ 1 ].MaxPktSize = EP_MAXPKTSIZE;
    ep_record[ 1 ].Interval  = EP_POLL;
    ep_record[ 1 ].sndToggle = bmSNDTOG0;
    ep_record[ 1 ].rcvToggle = bmRCVTOG0;
    Usb.setDevTableEntry( 1, ep_record );              //plug kbd.endpoint parameters to devtable
    /* Configure device */
    rcode = Usb.setConf( KBD_ADDR, 0, 1 );
    if( rcode ) {
        Serial.print("Error attempting to configure keyboard. Return code :");
        Serial.println( rcode, HEX );
        while(1);  //stop
    }
    /* Set boot protocol */
    rcode = Usb.setProto( KBD_ADDR, 0, 0, 0 );
    if( rcode ) {
        Serial.print("Error attempting to configure boot protocol. Return code :");
        Serial.println( rcode, HEX );
        while( 1 );  //stop
    }
    
    delay(2000);
    Serial.println("Lyklabord virkt");
}
/* Poll keyboard and print result */
void kbd_poll( void )
{
  char i, j;
  char buf[ 8 ] = { 0 };      //keyboard buffer
  static char old_buf[ 8 ] = { 0 };  //last poll
  byte rcode = 0;     //return code
    /* poll keyboard */
    rcode = Usb.inTransfer( KBD_ADDR, KBD_EP, 8, buf );
    if( rcode != 0 ) {
        return;
    }//if ( rcode..
    for( i = 0; i < 8; i++ ) {
        if( buf[ i ] != old_buf[ i ] ) {
            for( j = 0; j < 8; j++ ) {
              do {
            switch( buf[ j ] ) {
            case buttonUp:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);        
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
         break;
            }
              }
              while( buttonUp == HIGH );
              
              do {
            switch( buf[ j ] ) {              
            case buttonDown:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, LOW);
          delay(20);
         break;
            }
              }
              while( buttonDown == HIGH );
              
              do {
            switch( buf[ j ] ) {              
            case buttonLeft:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
         break;
            }
              }
              while( buttonLeft == HIGH );              
              do {
            switch( buf[ j ] ) {              
            case buttonRight:
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, LOW);
          digitalWrite(motorPin6, HIGH);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, HIGH);
          digitalWrite(motorPin8, LOW);
          delay(20);
          digitalWrite(motorPin1, LOW);
          digitalWrite(motorPin5, LOW);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, HIGH);
          digitalWrite(motorPin7, HIGH);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
          digitalWrite(motorPin1, HIGH);
          digitalWrite(motorPin5, HIGH);
          digitalWrite(motorPin2, HIGH);
          digitalWrite(motorPin6, LOW);
          digitalWrite(motorPin3, LOW);
          digitalWrite(motorPin7, LOW);
          digitalWrite(motorPin4, LOW);
          digitalWrite(motorPin8, HIGH);
          delay(20);
         break;
            }
              }
     while( buttonRight == HIGH );
     
            }                       
                old_buf[ j ] = buf[ j ];
            }//for( j = ...
        }//if( buf...        
    }// for( i = 0...