Stepper motor is not rotating 200 steps using micros()

I wrote a simple stepper motor program using micros() , it runs clockwise or anticlockwise as expected , but when i try to run the motor for just one complete revolution , it doesn't rotate and also when i try to run it 200 steps for clockwise and 200 steps for anticlockwise , it just rotates clockwise continuously

The below program is for running it for one complete revolution, which is not happening . Circuit is not the issue , since i tried using other stepper program using delaymicroseconds() and it works as expected ( one revolution) .

I am using drv8225 , 1.8 step stepper motor

byte dirPin = 4;
byte stepPin = 3 ;
unsigned long curMills;
unsigned long prevStepMills = 0;
unsigned long millisBetweenSteps = 2000;
int k;

int sleep=5;
int reset=6; 
int enable=7;
int gnd=8;
int detect=2;
int ByteReceived;
void setup() {
Serial.begin(9600);
  pinMode(sleep,OUTPUT);
  pinMode(reset,OUTPUT);
  pinMode(enable,OUTPUT); 
  pinMode(gnd,OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(detect, INPUT);
  k=1;

  

}

void loop() {

  digitalWrite(sleep,HIGH);
  digitalWrite(reset,HIGH);
  digitalWrite(enable,LOW);
  digitalWrite(gnd,LOW);

  curMills = micros();
  
 if (k==1)
 {
  for(int i =0;i<=200;i++)
  {
   
   clockwise();
   Serial.println(i);
  }


 k=k+1;
}

}
void clockwise()
{ digitalWrite(dirPin,LOW);
   if((curMills-prevStepMills) >= millisBetweenSteps)
   {
    prevStepMills = curMills;
    
    
    digitalWrite(stepPin,HIGH);
    
    digitalWrite(stepPin,LOW);
   
   }
}
void cclockwise()
{digitalWrite(dirPin,HIGH);
   if((curMills-prevStepMills) >= millisBetweenSteps)
   {
    prevStepMills = curMills;
    
    digitalWrite(stepPin,HIGH);
    
    digitalWrite(stepPin,LOW);
   
   }
}

thanks in advance

Why are you using micros() when the program seems to have been designed for millis()?

Does it work when you use millis()? If so my guess is that you are trying to rotate the motor too quickly and it is missing steps.

And when you change a program (for example to use micros() in place of millis()) change the names of the variables so they relate correctly to the new situation. It is just confusing to use a variable called curMills to hold a number of microseconds and that sort of confusion leads to mistakes.

...R

What's the purpose of the variable k? Its only an int and will overflow/wrap in milliseconds
with loop() running constantly.

Have you tried out the examples in AccelStepper and Stepper libraries?

Yep.... this is another case of not learning to crawl before learning to walk, and not learning to walk before learning to run. Try to go for the "start small, and then work to bigger things later" idea.

To begin with, it will be good to learn how to drive something simple, like an LED.... eg.... make the LED turn on for 1 second, and turn off again. That's the 'blink' tutorial (or something like that). This will guarantee that your time intervals are correct. Otherwise, if you don't want to visualise LEDs, then use the serial monitor window (with serial.println functions) to print some text every 1 second...to ensure that the time-base is correct.

The next thing to do is to write the simplest code that you can .... to make the stepper do 1 step for every time that the LED transitions from OFF to ON.

If you hear little ticks from your stepper motor every couple of seconds or so, then it means your stepper motor is stepping.

If it takes say 2 seconds to step just one time, and if your motor takes 200 steps to complete 1 revolution, then your motor should be finished in 400 seconds.... nearly 7 minutes. So if a marker flag is mounted on the stepper shaft... then you can get an idea about whether or not the motor is doing the full 200 steps....as 1 rotation will be completed after 200 turn-on cycles of your LED.

And then.....after this slow stepping is confirmed to work, you could then increase the speed and test the limits of stepping rate etc .... ie. how fast can it step without undesired results like missing steps.

Nidmar1107:
I did use millis() initially , but the motor vibrates while rotating , micros() gave a better response .
The below code is what i am trying to do using micros() or millis() , two proximity sensor one for each motor .

The motors runs simultaneously . but when either sensor gets triggered the corresponding motor runs anticlockwise for 400 steps and 400 step clockwise and then stop.

I am trying to perform the same function using millis() or micros() .

The below code uses delay, hence the second motor stops rotating , when the proximity sensor of the first motor is triggered and the first motor rotates 400 steps .

I apologize for the inconvenience , I am new to this forum hence didn't know how to use the tags.

#include<Stepper.h>

const int stepPin = 3;
const int dirPin = 4;
const int stepPin1 = 10;
const int dirPin1= 11;
int sleep=5;
int reset=6;
int enable=7;
int gnd=8;
int detect=2;
int detect1=12;
int rpm=1000;
Stepper stepper(200,stepPin,dirPin);
Stepper stepper1(200,stepPin1,dirPin1);
void setup() {
  // Sets the two pins as Outputs

pinMode(sleep,OUTPUT);
  pinMode(reset,OUTPUT);
  pinMode(enable,OUTPUT);
  pinMode(gnd,OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(detect, INPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin1, OUTPUT);
  pinMode(detect1, INPUT);
  stepper.setSpeed(rpm);
  stepper1.setSpeed(rpm);
}
void loop() {
  digitalWrite(sleep,HIGH);
  digitalWrite(reset,HIGH);
  digitalWrite(enable,LOW);
  digitalWrite(gnd,LOW);
  digitalWrite(dirPin, HIGH);
  digitalWrite(dirPin1, HIGH);
  stepper1.step(1);
  stepper.step(1);
  if(digitalRead(detect)==LOW)
  {
    digitalWrite(dirPin, LOW); 
    for(int i=0;i<400;i++)
    {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);
    }
    digitalWrite(dirPin, HIGH);
    for(int i=0;i<400;i++)
    {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);
    }
    //dig
    stepper.step(0);

}

else if(digitalRead(detect1)==LOW)
  {
    digitalWrite(dirPin1, LOW); 
    for(int i=0;i<400;i++)
    {
    digitalWrite(stepPin1,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1,LOW);
    delayMicroseconds(2000);
    }
    digitalWrite(dirPin1, HIGH);
    for(int i=0;i<400;i++)
    {
    digitalWrite(stepPin1,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1,LOW);
    delayMicroseconds(2000);
    }
    //dig
    stepper1.step(0);

}

}

Please edit your Reply #5 and use the tags properly. No tags around your own text. Quote tags around text you are quoting from another Reply and Code tags around code.

It is very difficult to make sense of your Reply at the moment.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What drive are you using? links to spec/data please.
What stepper are you using? links to spec/data please.

Thanks.. Tom.. :slight_smile: