Running multiple motors for x amount of time with two buttons

Hello, newbie here!

I wan’t to run two motors, with three buttons. One button for motor 1, one button for motor 2 and the last one is emergency stop for both.

I can only get the first motor to run, and not the second. The emergency stop also works. I tried naming the motors driveStepper1 and driveStepper2, but that doesn’t work. I can upload the program to my Arduino. Does anyone know where in my code I am doing something wrong?

Thanks in advance!

//end

// motor 1
int Pin1 = 4; //IN1 is connected to 4
int Pin2 = 5; //IN2 is connected to 5
int Pin3 = 6; //IN3 is connected to 6
int Pin4 = 7; //IN4 is connected to 7
int switch1 = 2; //red button left
int switch2 = 3; //yellow button

//motor 2
int Pin5 = 8; //IN1 is connected to 8
int Pin6 = 9; //IN2 is connected to 9
int Pin7 = 10; //IN3 is connected to 10
int Pin8 = 11; //IN4 is connected to 11
int switch3 = 12; //red button right

int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values


int poleStep = 0; 
int dirStatus = 3; // motor status 3 = stop (don't change)
int unsigned long time_now=0;
int period=2000; //(runtime in milliseconds)

void setup() 
{ 
//motor 1
pinMode(Pin1, OUTPUT); //Define output
pinMode(Pin2, OUTPUT);   
pinMode(Pin3, OUTPUT);   
pinMode(Pin4, OUTPUT);
pinMode(switch1,INPUT_PULLUP); // Define input
pinMode(switch2,INPUT_PULLUP); //

//motor 2
pinMode(Pin5, OUTPUT);
pinMode(Pin6, OUTPUT);
pinMode(Pin7, OUTPUT);
pinMode(Pin8, OUTPUT);
pinMode(switch3, INPUT_PULLUP);  

} 
void loop() 
{ 

if(digitalRead(switch1) == LOW) //If red button is pressed, motor status = 1 and motor drives clockwise
{
  dirStatus =1;
  time_now=millis(); // time starts at 0
  

}else if(digitalRead(switch2) == LOW) //íf yellow button is pushed, motor status = 3 and motor 1 and 2 stops
{
  dirStatus =3;
}
if((dirStatus == 1) && (millis()<time_now+period)){ //If motor status is = 1, the motor runs in x amount of milliseconds
 poleStep++; // Motor runs clockwise
  driveStepper1(poleStep);

//motor 2
if(digitalRead(switch3) == LOW) 
{dirStatus = 1;
time_now=millis();

}else if(digitalRead(switch2) == LOW) //íf yellow button is pushed, motor status = 3 and motor 1 and 2 stops
{
  dirStatus =3;
}
if((dirStatus == 3) && (millis()<time_now+period)) {
  poleStep++;
  driveStepper2(poleStep);
}
        
}else{
driveStepper1(8); //motor 1
}
if(poleStep>7){ 
 poleStep=0; 
} 
if(poleStep<0){ 
 poleStep=7; 
 
 
}driveStepper2(8); //motor 2
if(poleStep>7){ 
 poleStep=0; 
} 
if(poleStep<0){ 
 poleStep=7;
} 
delay(1); //Update every millisecond

}// loop


void driveStepper1(int c)
{
// motor 1
   digitalWrite(Pin1, pole1[c]);  
   digitalWrite(Pin2, pole2[c]); 
   digitalWrite(Pin3, pole3[c]); 
   digitalWrite(Pin4, pole4[c]);


}
void driveStepper2(int c)
{
// motor 2
   digitalWrite(Pin5, pole1[c]);  
   digitalWrite(Pin6, pole2[c]); 
   digitalWrite(Pin7, pole3[c]); 
   digitalWrite(Pin8, pole4[c]);
}

Look at the example BlinkWithoutDelay.

That is a program explaining how to do multiple things at once.

.

fedefrede69:
Does anyone know where in my code I am doing something wrong?

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent your code consistently for easier reading.

In the meantime have a look at how millis() is used to manage timing without blocking in Several Things at a Time. And see Using millis() for timing. A beginners guide if you need more explanation.

You may find it easier to create your program if you use the AccelStepper library as it handles all the housekeeping.

...R

ieee488:
Look at the example BlinkWithoutDelay.

That is a program explaining how to do multiple things at once.

.

Found this video about BlinkWithoutDelay, which helped a lot! - Understanding Blink Without Delay - YouTube

Now I stumbled onto a new problem :slight_smile:

When I press left button (switch1), motor 1 runs. When I press right button(switch3), motor 2 runs. Now if I press left button again, both motor 1 and motor 2 will run. It will reset if I press the emergency button (switch2), and I can press left button (switch1) and run motor 1 without running motor 2. Where does it go wrong?

// motor 1
int Pin1 = 4; //IN1 is connected to 4
int Pin2 = 5; //IN2 is connected to 5
int Pin3 = 6; //IN3 is connected to 6
int Pin4 = 7; //IN4 is connected to 7
int switch1 = 2; //red button left - motor 1 start
int switch2 = 3; //yellow button - emergengcy stop

//motor 2
int Pin5 = 8; //IN1 is connected to 8
int Pin6 = 9; //IN2 is connected to 9
int Pin7 = 10; //IN3 is connected to 10
int Pin8 = 11; //IN4 is connected to 11
int switch3 = 12; //red button right - motor 2 start
 
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values


int poleStep1 = 0;
int poleStep2 = 0; 
int dirStatus1 = 3; // motor status 3 = stop (don't change)
int dirStatus2 = 3; 
int unsigned long time_now=0;
int period=2000; //(runtime in milliseconds)

void setup() 
{ 
  //motor 1
 pinMode(Pin1, OUTPUT); //Define output
 pinMode(Pin2, OUTPUT);   
 pinMode(Pin3, OUTPUT);   
 pinMode(Pin4, OUTPUT);
 pinMode(switch1,INPUT_PULLUP); // Define input
 pinMode(switch2,INPUT_PULLUP); 

//motor 2
pinMode(Pin5, OUTPUT);
pinMode(Pin6, OUTPUT);
pinMode(Pin7, OUTPUT);
pinMode(Pin8, OUTPUT);
pinMode(switch3, INPUT_PULLUP);  

} 
 void motor1() 
{ 
  //MOTOR 1
  if(digitalRead(switch1) == LOW) //If red button is pressed, motor status = 1 and motor drives clockwise
  {
    dirStatus1 =1;
    time_now=millis(); // time starts at 0
    
  }else if(digitalRead(switch2) == LOW) //íf yellow button is pushed, motor status = 3 and motor 1 and 2 stops
  {
    dirStatus1 =3;
  }
  if((dirStatus1 == 1) && (millis()<time_now+period)){ //If motor status is = 1, the motor runs in x amount of milliseconds
   poleStep1++; // Motor runs clockwise
    driveStepper1(poleStep1);        
 }else{
  driveStepper1(8); //motor 1
 }
 if(poleStep1>7){ 
   poleStep1=0; 
 } 
 if(poleStep1<0){ 
   poleStep1=7; 
 } 
 delay(1); //Update every millisecond
}

 void motor2() 
{ 
  //MOTOR 2
  if(digitalRead(switch3) == LOW) //If red button is pressed, motor status = 1 and motor drives clockwise
  {
    dirStatus2 =1;
    time_now=millis(); // time starts at 0
    
  }else if(digitalRead(switch2) == LOW) //íf yellow button is pushed, motor status = 3 and motor 1 and 2 stops
  {
    dirStatus2 =3;
  }
  if((dirStatus2 == 1) && (millis()<time_now+period)){ //If motor status is = 1, the motor runs in x amount of milliseconds
   poleStep2--; // Motor runs clockwise - polerne er omvendt
    driveStepper2(poleStep2);        
 }else{
  driveStepper2(8); //motor 1
 }
 if(poleStep2>7){ 
   poleStep2=0; 
 } 
 if(poleStep2<0){ 
   poleStep2=7; 
 } 
 delay(1); //Update every millisecond
}

void loop()
{
 motor1();
 motor2(); 
}

void driveStepper1(int c)
{
  // motor 1
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);


}
void driveStepper2(int c)
{
  // motor 2
     digitalWrite(Pin5, pole1[c]);  
     digitalWrite(Pin6, pole2[c]); 
     digitalWrite(Pin7, pole3[c]); 
     digitalWrite(Pin8, pole4[c]);
}
//end

Rather than this

void loop()
{
 motor1();
 motor2(); 
}

I suggest you do it like this

void loop()
{
 checkSwitches();
 motor1();
 motor2(); 
}

so that you separate the business of getting the switch values from the business of operating the motors. Save the switch values to variables and then check those variables to decide which motor should do what.

It will also mean that you can print the switch values as part of your debugging.

...R
Several Things at a Time