Stepper motors acting in unison with pan/tilt system (weird coding problem)

Hi all,
I'm fairly early in the process of building a pan / tilt, 2 axis analog joystick controlled system, open-loop style with stepper motors for now (because I already have a couple beefy 1.8º steppers). I'm driving them with a couple of Big Easy Drivers from Sparkfun (Big Easy Driver - ROB-12859 - SparkFun Electronics) and Arduino Uno. My current issue is running both motors at the same time. One motor at a time is responding as expected with my mapping of the joystick values to use for microsecond delayed stepping of the motors via the Big Easy Drivers. However when both motors are engaged they act in unison, responding to the lowest speed value (highest step delay value) of either axis of the joystick; additionally, then running both motors at half the speed either motor would run solo at the same, mapped pot(joystick) value.

I have tried the following:

-reassigning output pins and physically distancing each of the motor control wires.

-separately feeding power to each of the stepper motors.
-and on that note: when supplying power to only one stepper motor (to it's driver), the problematic results persist,
leading me to believe it is a programing/coding issue.

-for now, I'm stepping in full steps, all microstepping yields the same relative results.

-moderate to thoroughly searched Arduino forums and google for this issue.

Thank You ahead of time for any help,
andrew

here is my code (not using libraries):

int stepPin1 = 5;
int dirPin1 = 6;
int M1_1 = 2;
int M1_2 = 3;
int M1_3 = 4;
int stepPin2 = 12;
int dirPin2 = 13;
int M2_1 = 9;
int M2_2 = 10;
int M2_3 = 11;
int pot1;
int pot2;
int panSpeed1;
int panSpeed2;
int tiltSpeed1;
int tiltSpeed2;
int minStepDly = 900;                   // was 5000    then 1500
int maxStepDly = 7000;                  // was 15000   then 16000
unsigned long currentMillis = 0;
unsigned long currentMillis2 = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
boolean flag;
void setup() {
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(M1_1, OUTPUT);
  pinMode(M1_2, OUTPUT);
  pinMode(M1_3, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(M2_1, OUTPUT);
  pinMode(M2_2, OUTPUT);
  pinMode(M2_3, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  digitalWrite(M1_1, HIGH);        // these are for microstepping the Big Easy Driver...
  digitalWrite(M1_2, LOW);
  digitalWrite(M1_3, LOW);
  digitalWrite(M2_1, HIGH);
  digitalWrite(M2_2, LOW);
  digitalWrite(M2_3, LOW);        // ... both set to full stepping for now
 Serial.begin(9600);
}
void loop() {
currentMillis = millis();
currentMillis2 = millis();
if (currentMillis - previousMillis >= 20 && flag == true){
  pot1 = analogRead(A0);
  panSpeed1 = map(pot1, 544, 1023, maxStepDly, minStepDly);
  panSpeed2 = map(pot1, 0, 480, minStepDly, maxStepDly);
  previousMillis = currentMillis;
  flag = false;
  }
if (currentMillis - previousMillis >= 20 && flag == false){
  pot2 = analogRead(A1);
  tiltSpeed1 = map(pot2, 544, 1023, maxStepDly, minStepDly);
  tiltSpeed2 = map(pot2, 0, 480, minStepDly, maxStepDly);
  previousMillis = currentMillis;
  flag = true;
  }
//deBugging();
if (pot1 <= 480){
  digitalWrite(dirPin1, HIGH);
  digitalWrite(stepPin1, HIGH);
  delayMicroseconds(panSpeed2);
  digitalWrite(stepPin1, LOW);
  delayMicroseconds(panSpeed2);
  }
if (pot1 >= 544){
  digitalWrite(dirPin1, LOW);
  digitalWrite(stepPin1, HIGH); 
  delayMicroseconds(panSpeed1);
  digitalWrite(stepPin1, LOW);
  delayMicroseconds(panSpeed1);
  }
if (pot1 <= 544 && pot1 >= 480){
  digitalWrite(stepPin1, LOW);
  }
if (pot2 <= 480){
  digitalWrite(dirPin2, LOW);
  digitalWrite(stepPin2, HIGH);
  delayMicroseconds(tiltSpeed2);
  digitalWrite(stepPin2, LOW);
  delayMicroseconds(tiltSpeed2);
  }
if (pot2 >= 544){
  digitalWrite(dirPin2, HIGH);
  digitalWrite(stepPin2, HIGH); 
  delayMicroseconds(tiltSpeed1);
  digitalWrite(stepPin2, LOW);
  delayMicroseconds(tiltSpeed1);
  }
if (pot2 <= 544 && pot2 >= 480){
  digitalWrite(stepPin2, LOW);
  }
}
void deBugging(){
if (currentMillis2 - previousMillis2 >= 500){
  Serial.print("pot1= ");
  Serial.print(pot1);
  Serial.print("\t panSpeed1/panSpeed2= ");
  Serial.print(panSpeed1);
  Serial.print("/");
  Serial.print(panSpeed2);
  Serial.print("\t pot2= ");
  Serial.print(pot2);
  Serial.print("\t tiltSpeed1/tiltSpeed2= ");
  Serial.print(tiltSpeed1);
  Serial.print("/");
  Serial.println(tiltSpeed2);
  previousMillis2 = currentMillis2;
  }
}

I think your problem is delay related.

Think like this, every time you use delay() in your code, nothing else runs... n-o-t-h-i-n-g. Go through your code and see if that makes sense regarding your problem.

The best, or the best that comes to my mind at the moment, is to set up a timer with an interrupt that is called at specific intervals and then you run your motor related code inside, or use the interrupt to keep track of pulses (steps) that your motor did.This way there is no need to delay your program.

I totally see whats going on with that now... of course that's how it would behave! Thank you!
I haven't quite grasped interrupts, etc. yet but I just went deeper with referring to millis(micros actually) to avoid delay.
This now works! So... many thank you's for your insight. However I feel that there is a cleaner, slicker way of doing this?
Any quick words or further info to point me in the right direction for tightening this code up?
Thanks,
~a

int stepPin1 = 5;
int dirPin1 = 6;
int M1_1 = 2;
int M1_2 = 3;
int M1_3 = 4;
int stepPin2 = 12;
int dirPin2 = 13;
int M2_1 = 9;
int M2_2 = 10;
int M2_3 = 11;
int pot1;
int pot2;
int panSpeed1;
int panSpeed2;
int tiltSpeed1;
int tiltSpeed2;
int minStepDly = 900;                   // was 5000    then 1500
int maxStepDly = 7000;                  // was 15000   then 16000
unsigned long currentMillis = 0;
unsigned long currentMillis2 = 0;
unsigned long currentMicros = 0;
unsigned long currentMicros2 = 0;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMicros = 0;
unsigned long previousMicros2 = 0;
boolean flag;
boolean flag2 = true;
boolean flag3 = true;
void setup() {
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(M1_1, OUTPUT);
  pinMode(M1_2, OUTPUT);
  pinMode(M1_3, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(M2_1, OUTPUT);
  pinMode(M2_2, OUTPUT);
  pinMode(M2_3, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  digitalWrite(M1_1, HIGH);        // these are for microstepping the Big Easy Driver...
  digitalWrite(M1_2, LOW);
  digitalWrite(M1_3, LOW);
  digitalWrite(M2_1, HIGH);
  digitalWrite(M2_2, LOW);
  digitalWrite(M2_3, LOW);        // ... both set to full stepping for now
 Serial.begin(9600);
}
void loop() {
currentMillis = millis();
currentMillis2 = millis();
if (currentMillis - previousMillis >= 20 && flag == true){
  pot1 = analogRead(A0);
  panSpeed1 = map(pot1, 544, 1023, maxStepDly, minStepDly);
  panSpeed2 = map(pot1, 0, 480, minStepDly, maxStepDly);
  previousMillis = currentMillis;
  flag = false;
  }
if (currentMillis - previousMillis >= 20 && flag == false){
  pot2 = analogRead(A1);
  tiltSpeed1 = map(pot2, 544, 1023, maxStepDly, minStepDly);
  tiltSpeed2 = map(pot2, 0, 480, minStepDly, maxStepDly);
  previousMillis = currentMillis;
  flag = true;
  }
//deBugging();
currentMicros = micros();
currentMicros2 = micros();
if (pot1 <= 480){
  if (currentMicros - previousMicros >= panSpeed2 && flag2 == true){
    digitalWrite(dirPin1, HIGH);
    digitalWrite(stepPin1, HIGH);
    previousMicros = currentMicros;
    flag2 = false;
    }
  if (currentMicros - previousMicros >= panSpeed2 && flag2 == false){
    digitalWrite(stepPin1, LOW);
    previousMicros = currentMicros;
    flag2 = true;
    }
  }
if (pot1 >= 544){
  if (currentMicros - previousMicros >= panSpeed1 && flag2 == true){
    digitalWrite(dirPin1, LOW);
    digitalWrite(stepPin1, HIGH); 
    previousMicros = currentMicros;
    flag2 = false;
    }
  if (currentMicros - previousMicros >= panSpeed1 && flag2 == false){
    digitalWrite(stepPin1, LOW);
    previousMicros = currentMicros;
    flag2 = true;
    }
  }
if (pot1 <= 544 && pot1 >= 480){
  digitalWrite(stepPin1, LOW);
  flag2 = true;
  }
if (pot2 <= 480){
  if (currentMicros2 - previousMicros2 >= tiltSpeed2 && flag3 == true){
    digitalWrite(dirPin2, LOW);
    digitalWrite(stepPin2, HIGH);
    previousMicros2 = currentMicros;
    flag3 = false;
    }
  if (currentMicros2 - previousMicros2 >= tiltSpeed2 && flag3 == false){  
    digitalWrite(stepPin2, LOW);
    previousMicros2 = currentMicros;
    flag3 = true;
    }
  }
if (pot2 >= 544){
  if (currentMicros2 - previousMicros2 >= tiltSpeed1 && flag3 == true){ 
    digitalWrite(dirPin2, HIGH);
    digitalWrite(stepPin2, HIGH); 
    previousMicros2 = currentMicros;
    flag3 = false;
    }
  if (currentMicros2 - previousMicros2 >= tiltSpeed1 && flag3 == false){
    digitalWrite(stepPin2, LOW);
    previousMicros2 = currentMicros;
    flag3 = true;
    }
  }
if (pot2 <= 544 && pot2 >= 480){
  digitalWrite(stepPin2, LOW);
  flag3 = true;
  }
}
void deBugging(){
if (currentMillis2 - previousMillis2 >= 500){
  Serial.print("pot1= ");
  Serial.print(pot1);
  Serial.print("\t panSpeed1/panSpeed2= ");
  Serial.print(panSpeed1);
  Serial.print("/");
  Serial.print(panSpeed2);
  Serial.print("\t pot2= ");
  Serial.print(pot2);
  Serial.print("\t tiltSpeed1/tiltSpeed2= ");
  Serial.print(tiltSpeed1);
  Serial.print("/");
  Serial.println(tiltSpeed2);
  previousMillis2 = currentMillis2;
  }
}

If you've got two near-identical items like two stepper motors, differing only by the pins they are controlled by, this is almost a certain application of:

  1. a single set of functions to control a single motor, given
  2. some data structure or structures containing a description (i.e pin numbers) of each motor and its current state.

This will ease your debugging and make your sketch considerably shorter and more maintainable.