need motor to stop after delay and wait for input

hey guys, i am trying to set up my Arduino to run 2 lego switch tracks for my daughter. eventually 4. i want to have toggle switches to switch the track. but the problem i am having is the motor will continue to run then stop then run again. i need it to get it to stop after a few seconds and then wait for the switch to be flipped again then run motor in the other direction for a few sec then wait again.

int LEDPin=8;
int buttonPin=12;
int buttonRead;
int dt=200;
const int IN1=7;
const int IN2=6;
const int ENB=5;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (LEDPin,OUTPUT);
pinMode(buttonPin,INPUT);
pinMode (ENB,OUTPUT);
pinMode (IN1,OUTPUT);
pinMode(IN2,OUTPUT);
analogWrite(ENB,255);
}

void loop() {
// put your main code here, to run repeatedly:
buttonRead=digitalRead(buttonPin);

delay(dt);
if (buttonRead==1){
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
delay(dt);
digitalWrite(IN2,LOW);
}

else{

digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
delay(dt);
digitalWrite(IN1,LOW);

}

}

A stop and wait for input would be like

while(digitalRead(buttonPin) != 1);

based on your button code.

Regarding the toggle switches, please post a wiring diagram of your idea. Only then will it be possible to comment meaningfully on it.

awesome thank you. i will draw one up and post it later.

Use the auto format function under tools in the ide to keep your formatting right. Look at getting rid of the delays as they prevent your code from being responsive to inputs. Look up ‘blink without delay’, ‘doing several things at once’ and state machines as it will make things easier. If you do the basic tutorials on these you will get it. You need to stop thinking in a step by step approach and think in a state approach. The code loops over and over and should check the state variable every time and perform the function associated with it.

I.e not this:
If button pressed
Start motor
Wait a second
Do something else
(Code takes 1.001 seconds and checks button once)

More like this
Check button
If button pressed set state to opposite state
If state 1 Run motor
Else stop motor
(Code takes 0.001 seconds and checks button again)

Just a crude example. You will need to add debounce code and timers etc

well i got it to work with one motor. now when i hook up a second motor and button press the first switch makes both motors spin then stop and the the other button makes them spin the other way when pressed then stops. here is my code so far. the attachment is a wire diagram of my current setup

int buttonPin2 = 13;
int buttonPin = 12;
int buttonRead;
int buttonRead2;
int dt = 100;
const int IN1 = 7;
const int IN2 = 6;
const int ENB = 5;
const int IN3 = 4;
const int IN4 = 3;
const int ENA = 2;

int LEDPin = 8;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (LEDPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode (ENB, OUTPUT);
pinMode (IN1, OUTPUT);
pinMode(IN2, OUTPUT);
analogWrite(ENB, 255);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
analogWrite(ENA, 255);
pinMode(buttonPin2, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
buttonRead = digitalRead(buttonPin);
Serial.println(buttonRead);
Serial.println(buttonRead2);
while (digitalRead(buttonPin) != 1);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
delay(dt);
digitalWrite(IN2, LOW);

while (digitalRead(buttonPin) != 0);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(dt);
digitalWrite(IN1, LOW);

buttonRead2 = digitalRead(buttonPin2);

while (digitalRead(buttonPin2) != 1);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(dt);
digitalWrite(IN4, LOW);

while (digitalRead(buttonPin2) != 0);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(dt);
digitalWrite(IN3, LOW);

}

This is the arduino while syntax

while (condition) {
  // statement(s)
}

Whiles should be avoided. Look at my previous post