Running stepper forward and reverse for time interval with delay.

I am trying to run the stepper motor for 5 sec then, wait for 2 secs, and reverse and run the motor again for 5 secs. But I am getting confused in the time difference and the delay logic something is going wrong.

Here is the code:

#include <Stepper.h>

#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
#define MAX_SPEED 16 // can't go faster than this. Keep within range
#define ALARMPIN 13 // to show action

Stepper stepper(STEPS, 9, 11, 10 ,12);
int previousTime = 0;
long eventTimer = 5000; // Run for duration in ms.
int stepDir = STEPS;
void setup() {
  // nothing to do
  Serial.begin(9600);
  pinMode(ALARMPIN, OUTPUT);
  
}

void loop() {
  long currentTime = millis();
  if(  (currentTime - previousTime) <  eventTimer) {
    setMotorInMotion();
    previousTime = currentTime;
  } else {
    delay(2000);
    eventTimer = eventTimer + eventTimer; 
    stopMotorMotion();
    if(stepDir == STEPS) {
      stepDir = -STEPS;
    } else {
      stepDir = STEPS;
    }
  }
}

void setMotorInMotion() {
  digitalWrite(ALARMPIN, HIGH);
  stepper.setSpeed(MAX_SPEED);
  stepper.step(stepDir); 
  Serial.println("Motor is running...");
}

void stopMotorMotion() {
  digitalWrite(ALARMPIN, LOW);
  stepper.setSpeed(0);
  stepper.step(0); 
  Serial.println("Motor Stopped");
}

Can someone suggest what the issue is?

I suspect part of your problem is the name of your function setMotorInMotion(). A better name would be implementMotorMove() because the move will be completed before the function returns.

Don't mix delay() with millis() because it can screw up the time keeping.

If you want a motor to move for a particular length of time EITHER figure out how many steps should happen in that time and get it to move that number of steps OR get it to move one step at a time with you controlling the interval between steps and also keeping an eye on the elapsed time,

You can do the latter with something like this pseudo code

void loop() {
   if motorRunning == false
            motorStartMillis = millis();
            motorRunning = true

   if motorRunning == true and millis() - motorStartMillis < runTime
       if it is time for the next step
            moveOneStep()
   else
      motorRunning = false

In some code of my own I have written my moveOneStep() function so it figures out whether it is time for another step and does nothing if the step is not due

...R