Problem with Micros() function !!!

The first time, I wish everyone have a nice day !
I’m writting a easy program to control stepper motor via A4988 driver. My program is when I insert a number on Serial monitor, the stepper motor will rotate exactly that revolutions. Everything is fine when I use delay() function. But when I use micros() function instead of delay(), a small problem appeared. When I insert a number on Serial monitor, the stepper motor rotated different position. Instead of rotate a revolution when I insert “1” on Serial monitor, the stepper just rotated a small angle. I wish I could get some experience from everyone ! Thank you all.
Here is my code:

float revnumber = 0;
unsigned long time = 0;
byte Enable = 6;
byte Step   = 5;
byte Dir    = 4;
//-----------------------------------------------------------------------
void control(){
 bool direct = LOW;
 revnumber = Serial.parseFloat();
 if(revnumber > 0){
   revnumber = revnumber*400;
   stepper(revnumber, direct);
 }
 else if(revenumber < 0){
   direct  = !direct;
   revnumber = - renumber*400;
   stepper(revnumber, direct);
 }
}

//-----------------------------------------------------------------------
void stepper(float rev, bool rotate){
 //for(int i=0; i<=rev; i++){          
   //if(micros() > time + 1000){           
     //digitalWrite(Enable,LOW);        
     //digitalWrite(Dir,rotate);          
     //analogWrite(Step,191);           
     //time = micros();             
   //}
 //}
 for(int i=0; i<=rev; i++){ 
   digitalWrite(Enable,LOW);
   digitalWrite(Dir,rotate);
   analogWrite(Step,191);
   delay(1);
 }
 digitalWrite(6,HIGH); 
}

//-----------------------------------------------------------------------
void setup() {
 pinMode(Enable,OUTPUT); // pin_6
 pinMode(Step,OUTPUT);   // pin_5
 pinMode(Dir,OUTPUT);    // pin_4
 Serial.begin(9600);
 Serial.println("Enter revolution number: ");
}

//-----------------------------------------------------------------------
void loop() {
 if(Serial.available() > 0) {
   control();
  }
}

  delay(1.02);Check the documentation for the delay function.

Please remember to use code tags when posting code.

If you want the full answer, post the full code, not just a snippet.

AWOL:
  delay(1.02);Check the documentation for the delay function.

Please remember to use code tags when posting code.

If you want the full answer, post the full code, not just a snippet.

Thank you so much ! :slight_smile:

These links may be of interest

Stepper Motor Basics
Simple Stepper Code

Serial Input Basics - simple reliable ways to receive data.

...R

Using micros() must be done from the loop() function, not in a separate loop. And rather than using a
for-loop to count the rotations you need to check in loop() how many iterations are left.

You seem to be confused about step+direction stepper drivers - the step signal is digital, you pulse it
once per step like this:

  digitalWrite (Step, HIGH) ;
  delayMicroseconds (2) ;
  digitalWrite (Step, LOW) ;

analogWrite continually puts out pulses at a fixed rate.

Robin2:
These links may be of interest

Stepper Motor Basics
Simple Stepper Code

Serial Input Basics - simple reliable ways to receive data.

...R

Thank you very much ! :slight_smile:

MarkT:
Using micros() must be done from the loop() function, not in a separate loop. And rather than using a
for-loop to count the rotations you need to check in loop() how many iterations are left.

You seem to be confused about step+direction stepper drivers - the step signal is digital, you pulse it
once per step like this:

  digitalWrite (Step, HIGH) ;

delayMicroseconds (2) ;
  digitalWrite (Step, LOW) ;




analogWrite continually puts out pulses at a fixed rate.

Thank you so much !!! :slight_smile: