Controlling a small stepper motor with the millis() command

Hi Guys

I am pretty new to this and am having a bit of trouble getting my stepper motor moving with the millis command. I had it working with the delay() command but as I want to do other things while the motor is waiting to move I thought I would try and get this working.

Here is my code.

const int motorPin1 = 8;
const int motorPin2 = 9;
const int motorPin3 = 10;
const int motorPin4 = 11;
const int shutterPin = 12;
long move1 = 100;
long move2 = 200;
long move3 = 300;
long move4 = 400;
long picHigh = 2400;
long picLow = 2500;
long theEnd = 12500;
long previousMillis = 0;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(shutterPin, OUTPUT);
}

void loop() {
    
  unsigned long currentMillis = millis();
    
    if(currentMillis - previousMillis > move1) {
      digitalWrite(motorPin1, HIGH);
      digitalWrite(motorPin2, LOW);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, HIGH);
    }
    
    if(currentMillis - previousMillis >= move2);{
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      digitalWrite(motorPin3, LOW);
      digitalWrite(motorPin4, HIGH);
    }
    
    if(currentMillis - previousMillis >= move3);{
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      digitalWrite(motorPin3, HIGH);
      digitalWrite(motorPin4, LOW);
    }
    
    if(currentMillis - previousMillis >= move4);{
      digitalWrite(motorPin1, HIGH);
      digitalWrite(motorPin2, LOW);
      digitalWrite(motorPin3, HIGH);
      digitalWrite(motorPin4, LOW);
    }
    
    if(currentMillis - previousMillis >= picHigh);{
      digitalWrite(shutterPin, HIGH);
    }
    
    if(currentMillis - previousMillis >= picLow);{
      digitalWrite(shutterPin, LOW);
    }
    
    if(currentMillis - previousMillis >= theEnd);{
      previousMillis = currentMillis;
    }
}

I get no movement at all at the moment, if that helps.

Thanks.

Mash

If currentMillis - previousMillis is 320 what happens? It is greater than 100 so that if fires, it is also greater than 200 so that if fires, too. And so on. I think that if you reverse the order of the checks it may work.

if(currentMillis - previousMillis > theEnd) else if(currentMillis - previousMillis > picLow) etc.

    if(currentMillis - previousMillis >= move2);{

Get rid of the semicolon - it makes the ‘if’ statement ineffective.

If you want to check for a value being in one of a set of possible ranges, you can do this with a cascading sequence of if/else statements. In your case the different values represent a logical sequence so it makes the code clearer if the order of your code matches the order of execution. You can achieve that by reversing the comparisons like this:

if(currentMillis - previousMillis < move1)
{
    // waiting for first move
}
else if (currentMillis - previousMillis < move2)
{
    // first move in progress
}
else if (currentMillis - previousMillis < move3)
{
    // second move in progress
   etc

If you use either the Stepper or AccelStepper libraries you will not have to figure out the pin settings for each phase of motor movement and your program will be much simpler.

And even better idea would be to use a proper stepper motor driver board that just needs a step and a direction connection to the Arduino - for example the Pololu A4988. I have no idea if it would be suitable for your motor but it will illustrate the idea.

...R

Thanks for your help. All I needed to do was add the esle if's and remove the semicolons. It is working fine now and makes sense.

I will look into the stepper librarys thanks for the heads up. I would also like to use a stepper motor driver but I am currently doing an Antarctic winter and do not have one, just a stepper motor and a darlington array IC.

On a side note do people think it would be better for me to use a Swtich Case instead of the ifs and else ifs?

Cheers again for your help.

Mash