Reversing a dc motor

I have been having a problem with reversing my dc motor. The motor is just running in the same direction and my program is not charging the direction as dictated by the sketch.
Someone to help me with the code to run the motor forward for some time the reverse for sometime and the stop the motor!

This is the code am using;

#define motorpin1 2
#define motorpin2 3
#define motorpin3 4
#define motorpin4 5
#define enablepin 8

void setup() {
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
  pinMode(motorpin3, OUTPUT);
  pinMode(motorpin4, OUTPUT);

  // set speedPin high so that motor can turn on:
  digitalWrite(enablepin, HIGH); 
}

void loop() {
    digitalWrite(motorpin1, LOW);   
    digitalWrite(motorpin2, HIGH);  
    digitalWrite(motorpin3, LOW);   
    digitalWrite(motorpin4, HIGH);
    delay(1500);
    digitalWrite(motorpin1, LOW);   
    digitalWrite(motorpin2, LOW);  
    digitalWrite(motorpin3, LOW);   
    digitalWrite(motorpin4, LOW);
    delay(1500);
    digitalWrite(motorpin1,HIGH);   
    digitalWrite(motorpin2,LOW);  
    digitalWrite(motorpin3,HIGH);   
    digitalWrite(motorpin4,LOW);
    
}

Why does the DC motor have 4 pins, or age you running two? Are you using a motor driver?

I am using two motors with a Pololu MD03A H-bridge motor driver.

I think what is happening is that you're reversing the motors at the end of the loop and then the loop repeats and immediately spins the motors forward again.

Try putting a delay(1500); just before the closing }

    digitalWrite(motorpin4,LOW);
    delay(1500); //<-- insert this line
}

HTH

Ver