Stepper motor spinning forever

I am using this driver A4988 http://mlb-d1-p.mlstatic.com/driver-motor-de-passo-a4988-stepstick-pololu-reprap-ramps-637001-MLB20252170560_022015-O.webp?square=false to run a NEMA 16 motor (4 wires bipolar).

I did all connections exactly like this diagram http://howtomechatronics.com/wp-content/uploads/2015/08/A4988-Wiring-Diagram.png?e739c9

The problem is this: I see some codes saying that sending a LOW (to the step pin) -> wait a few milliseconds -> sending a HIGH (to the step pin) will make it walk one step. But for some reason in my case it's completely different.

In my case when I do this once (yes, once):

      digitalWrite(step_pin,HIGH); 
      delayMicroseconds(800); 
      digitalWrite(step_pin,LOW); 
      delayMicroseconds(800);

my motor spins forever in a single direction. If I execute the above code again, my motor stops spinnins. It is not taking steps it's is rotating forever till I execute code above. I am using this code below:

const byte step_pin = 3;
const byte dir_pin = 4;

void setup() {

    pinMode(step_pin,OUTPUT);
    pinMode(dir_pin,OUTPUT);
    
    digitalWrite(step_pin,HIGH);
    digitalWrite(dir_pin,HIGH);

}

void loop() {

    char resultado = "";
    
    if (Serial.available()) {
    
      resultado = (char)Serial.read();
    
      if (resultado == "a") {
    
              digitalWrite(step_pin,HIGH); 
              delayMicroseconds(800); 
              digitalWrite(step_pin,LOW); 
              delayMicroseconds(800); 
              
        }
      
    }

}

What is going on? My motor should only spin a single step every time "a" is sent in the Serial, right? But no, it's spinning forever till next time I press "a" and it stops spinning. But if I hit "a" again my motor starts spinning forever again!

if (resultado == "a")This is comparing a [u]character[/u] to a [u]null-terminated string[/u] and is syntactically incorrect. The compiler allows it, but in my view it shouldn't. The results are unpredictable.

Have a look at the examples in Simple Stepper Code

Note that you do not have to spread the time equally between the HIGH and LOW parts of the pulse. The driver just needs a very short HIGH pulse. By putting all of the time in the LOW part it means you only need to change one number when you want to chanage the speed.

...R Stepper Motor Basics

@Robin2 ok, I removed the first delayMicroseconds and still works (so thank you man!) but my motor still spins forever, it does not make a single step as it should. Any idea why this is going on?

See reply #1