How to set X amount of time for a motor to stop and then X amount for it to run

Hi guys this is my first post,and im very trash at programming im trying to learn, this question might be stupid don't roast plz ,

so i need to run a stepper motor for a x amount of time by pressing a button and stop it for an amout of time by pressing a stop button,

im able to run it for x amount of time and stop it with stop button, what i need is enter time in code for it to run and when i press the stop button it stops for the certain amount of time and loop again

here is my code
#include <elapsedMillis.h>
#include <StepperDriver.h>
int motor_steps = 200; //
int step_divisition = 1;
int en_pin = 6;
int cw_pin = 4;
int clk_pin = 3;
const int buttonPin = 8;
const int buttonPinstop = 9;
int buttonState = 0;
int buttonStatestop = 0;
StepperDriver ss(motor_steps, step_divisition, en_pin, cw_pin, clk_pin);
unsigned int period =5000;
unsigned int period2 =2000;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(buttonPinstop, INPUT);
/ss.setSpeed(180);
ss.powerEnable(true);
ss.positioning();
delay(1600);
/
}
void loop() {
elapsedMillis timeElapsed;

buttonState = digitalRead(buttonPin);
buttonStatestop = digitalRead(buttonPinstop);
if (buttonState == HIGH) {
while (timeElapsed < period && buttonStatestop == LOW) {
Serial.println("test1");
ss.setSpeed(180);
ss.step(400);
delay(300);
ss.step(-400);
delay(300);
buttonState = digitalRead(buttonPin);
buttonStatestop = digitalRead(buttonPinstop);
}
}
}

sketch_jan21libraryc.ino (1.09 KB)

I'm not clear what you want to achieve because stepper motors usually move for a certain number of steps rather than a certain time. If you do want it to operate for a time period then IMHO it would be better to get it to move one step at a time during the period with your code controlling the interval between steps.

If you want a responsive program don't use delay(). Use millis() to manage timing as illustrated in Several Things at a Time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

Don't use WHILE, use IF. Allow the loop() function to do the repetition and then there is no need to duplicate the lines of code that read the buttons.

Rather than doing something if a button is pressed, do it when the button changes from not-pressed to pressed. You can use code like this

prevButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState !== prevButtonState and buttonState == LOW) {  // assumes LOW when pressed

...R

sorry i wasn't clear, what i meant by run is that the stepper will do few steps cw and few steps ccw repetitively to simulate mouvement

and what i want to do is when i enter time and press button the motor "runs" for that amount of time and when i enter stoppage time and press stop button it stops for that time and then repeat, thank you for your response.

Then I would save the value of millis() when the start button is pressed and keep checking to see if the ON interval has elapse. Then the interval has elapsed stop calling the code to move the motor.

I'm still unclear about the stop interval - won't it just stop until the next time you press the start button?

...R