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?
#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);
   }
}
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;
  Â
   } Â
}