Two motors working at the same time

Hello everyone!

I'm using Arduino UNO and I'm trying to make two DC motors run at the same time and if button is pressed one of the motors stops.
Here is my code but I can't figure out what is wrong (I'm pretty new with all this). Does anyone knows how to fix this? :confused:

#define I1 2
#define I2 3
#define BT 8
#define I3 4
#define I4 5
#define BT2 9
#define LED 13

const long interval = 30000;  
int state = LOW;
int buttonState, buttonState2;
unsigned long previousMillis = 0;   
void setup() {
  // put your setup code here, to run once:

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(BT, INPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
  pinMode(BT2, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
   buttonState = digitalRead(BT);
   buttonState2 = digitalRead(BT2);
   unsigned long currentMillis = millis();
   
   if(buttonState == LOW) {
   

      if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
   
       digitalWrite(I1, LOW);
       digitalWrite(I2, LOW);
       digitalWrite(LED, HIGH);
      }
      
      
   }
   else{
       digitalWrite(I1, HIGH);
       digitalWrite(I2, LOW);
       digitalWrite(LED, LOW);
      }

      
    if(buttonState2 == LOW) {
   

      if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
   
       digitalWrite(I3, LOW);
       digitalWrite(I4, LOW);
       digitalWrite(LED, HIGH);
      }
     
      
   }
   else{
       digitalWrite(I3, HIGH);
       digitalWrite(I4, LOW);
       digitalWrite(LED, LOW);
      }
}

You need to tell us what your code actually does.

What is the purpose of the line if (currentMillis - previousMillis >= interval)?

You use that twice. It seems to me you should only use it once and put it before the first if(buttonState == LOW)

If that is not appropriate then maybe you need a separate previousMillis and interval for each motor

...R

I finally figured out how to to this. This is the code :

#define I1 2
#define I2 3
#define BT 8
#define I3 4
#define I4 5
#define BT2 9
#define LED 13

const long interval = 3000;  
boolean st2 = false;
boolean st = false;
int buttonState, buttonState2;
unsigned long previousMillis = 0;  
unsigned long previousMillis2 = 0;    
void setup() {
  // put your setup code here, to run once:

  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(BT, INPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
  pinMode(BT2, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
   buttonState = digitalRead(BT);
   buttonState2 = digitalRead(BT2);
   unsigned long currentMillis = millis(); // First motor run time 
   unsigned long currentMillis2 = millis(); // Second motor run time
   
   if(buttonState == LOW) { 
    
       digitalWrite(I1, LOW);
       digitalWrite(I2, LOW);
       digitalWrite(LED, LOW);
       previousMillis = currentMillis;
       st = false;
      
   }
   
      if((st == false)&& (currentMillis - previousMillis >= interval)){
        
       digitalWrite(I1, HIGH);
       digitalWrite(I2, LOW);
       digitalWrite(LED, HIGH);
       st = true;
       
      }
// Second Motor
     if(buttonState2 == LOW) { 
    
       digitalWrite(I4, LOW);
       digitalWrite(I3, LOW);
       digitalWrite(LED, LOW);
       previousMillis2 = currentMillis2;
       st2 = false;
      
   }
   
      if((st2 == false)&& (currentMillis2 - previousMillis2 >= interval)){
        
       digitalWrite(I4, HIGH);
       digitalWrite(I3, LOW);
       digitalWrite(LED, HIGH);
       st2 = true;
       
      }    
 
}