Running stepper motor using millis() function

Hey guys,

I managed to run my stepper motor using millis() and it worked, and the code is as following:

void loop()
{
  unsigned long currentMillis = millis();
  //x = x*2;
  //if ( x > 0){ 
  
  if(currentMillis - previousMillis >= sped) {
    previousMillis = currentMillis;   
    if (stpState == LOW)
      stpState = HIGH;
    else
      stpState = LOW;
    digitalWrite(stp, stpState);
    digitalWrite(dir, 1);
    x--;
  }
 }
}

now i am trying to update the code to this version, using loop in order to move a specific degrees, but it does`nt work!!!

for (j=0; j<= 12000; j++)             // Keep looping until we hit the pre-defined max number of steps
      {
        y = 0;
        do
          {
            if(currentMillis - previousMillis >= X) 
            {
              previousMillis = currentMillis;   
              if (stpState == LOW)
                stpState = HIGH;
              else
                stpState = LOW;
              digitalWrite(stp, stpState);
              digitalWrite(dir, 1);
              y = 1;
            }
          }
        while (y = 0);
      }

Can anyone help me with this plz!!

        while (y = 0);

Once you assign 0 to y, of course y will be 0. = != ==.

Thx for the correction.

I just corrected the expression to (y == 0), but still nothing worked!!

I just corrected the expression to (y == 0), but still nothing worked!!

Of course the code works. That it does not do what you expect does not change that fact.

What it actually does is a mystery. What you want it to do is a mystery. What we need is an author to complete this mystery novel, so we can see how it ends.

One question. Once you determine the correct step direction, why do you need to set the step direction 12000 times? If you don't trust the pin to stay set, how can you be comfortable not telling it to be in some state 24000 or 36000 or 48000 times?

In order to run a stepper motor correctly, the clock pin needs to be alternated between (on/off) state, with time interval (x)

So, what i am trying to do with this code is to alternate the (stp pin) 1200 times.

i used to (do - while) loop to make sure the program will keep looping until the condition (currentMillis - previousMillis >= X) happens, and the (y) variable is just an indication variable to the loop that the condition happened.

Hope i answered your questions about my code.

In order to run a stepper motor correctly, the clock pin needs to be alternated between (on/off) state, with time interval (x)

So, why is the clock pin called dir?

(dir) is the direction pin.

amrahmed: (dir) is the direction pin.

Which you set 12000 times.

nope, 12000 is the no. of steps the stepper will step.

it can be changed to any no. according to the no. of revolutions the motor is required to do!!

amrahmed:
nope, 12000 is the no. of steps the stepper will step.

it can be changed to any no. according to the no. of revolutions the motor is required to do!!

for (j=0; j<= 12000; j++)             // Keep looping until we hit the pre-defined max number of steps
      {
        y = 0;
        do
          {
            if(currentMillis - previousMillis >= X) 
            {
              previousMillis = currentMillis;   
              if (stpState == LOW)
                stpState = HIGH;
              else
                stpState = LOW;
              digitalWrite(stp, stpState);
              digitalWrite(dir, 1);

When that for loop iterates, how many times will the last statement in that snippet be executed?

Personally, I find the nearly useless do/while statement pointless. The same thing can be done with a while statement.

I also find that testing time variables in a for loop to be a sign that you really don’t know what you are doing. It would appear that you want to take up to 12000 steps, if they can be completed in some period of time.

I think it is far easier to understand if get rid of the for loop, and simply say if there is still time AND less than 12000 steps have been taken, take another one.

Have a look at this simple stepper code. The second example uses millis() and micros().

Each step should involve a brief HIGH followed by a return to LOW. The speed should be determined by the interval between steps - not the length of the HIGH pulse.

...R Stepper Motor Basics

Hey Robin2,

What i am trying to do with my code (which basically works) is to toggle the HIGH/LOW state with every time period specified, the problem is it does not work when i put it inside a loop :( .

As i tried to explain, the do-while loop is used to make sure the program is trapped until the time interval passed, and to make sure that i did not lose step!

Kindly see my code again, and see if you can help me with your coding experience.

Thanks...

You are not calling millis() in the loop so it will never terminate.

Also you should set the direction pin before toggling the step pin, as the direction pin is read only when the step pin goes high.

This is the standard code to step a step/direction interface:

void step (bool direction)
{
  digitalWrite (dir_pin, direction) ;
  delayMicroseconds (2) ;  // probably not needed, but safe, allows for opto-coupler skew
  digitalWrite (step_pin, HIGH) ;
  delayMicroseconds (10) ;   // need the pulse width to be long enough for opto-coupler to respond, 10us is usual
  digitalWrite (step_pin, LOW) ;
}

Most stepper drivers have opto-coupler inputs and standard (photo transistor) opto-couplers are much slower than logic signals, hence the microseconds of delay. In particular they are slow to switch off, hence the normal practice of using a positive-going pulse.

Faster opto couplers are available, they cost more.

MarkT: You are not calling millis() in the loop so it will never terminate.

Thanks MarkT!

Actually this this the problem with my code.

It was my mistake that it did deal with millis() in a wrong way, i did deal with it as a variable, while this did cause a gr8 mistake that the value of "currentMillis" will never change and will always be the same!

Thanks alot man :)

amrahmed:
Kindly see my code again, and see if you can help me with your coding experience.

Have you looked at my code?
It works.
I don’t see the need for me to write an alternative version.

In very simple terms if you have a function that causes a step - for example

void oneStep() {
   digitalWrite(stepPin, HIGH);
   digitalWrite(stepPin,LOW);
}

then you just need a loop that calls that as required

if (stepCount < required){
   if (timeForStep) {
     oneStep();
     stepCount++;
   }
  }
}

…R