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);
}
}