Keeping motors move

Hi, I'm controlling 4 motors. It's working well. However, motor only moves when I'm pressing the button. Otherwise, motors stop. Also, motor move for ward for a bit and move backward for a bit continuously.

I want motors keep move forward when I press button one time, and keep move reverse when I press the button again.

Is there any way to keep the motor moves?

Thank you! this is my code

//set pin number
  int Button = 13;
  int pwm1 = 5;
  int pwm2 = 6;
  int pwm3 = 9;
  int pwm4 = 10;
  
  const int i1 = 0;
  const int i2 = 1;
  const int i3 = 2;
  const int i4 = 3;
  const int i5 = 7;
  const int i6 = 8;
  const int i7 = 11;
  const int i8 = 12;
  
  // Variables
  boolean state = LOW;
  int Counter = 0;

void setup() {
//Set pin I/O
  pinMode(Button, INPUT);

  pinMode(pwm1, OUTPUT);
  pinMode(pwm2, OUTPUT);
  pinMode(pwm3, OUTPUT);
  pinMode(pwm4, OUTPUT);
  pinMode(i1, OUTPUT);
  pinMode(i2, OUTPUT);
  pinMode(i3, OUTPUT);
  pinMode(i4, OUTPUT);
  pinMode(i5, OUTPUT);
  pinMode(i6, OUTPUT);
  pinMode(i7, OUTPUT);
  pinMode(i8, OUTPUT);
  digitalWrite(Button, LOW);
}


void loop() {
  state = digitalRead(Button);
  if (state = HIGH){
  Counter = Counter+1;
  delay(1000);    
  }
  //Descend if counter is odd
  if ( (Counter % 2) == 0) {
   analogWrite(pwm1, 255);
   analogWrite(pwm2, 255);
   analogWrite(pwm3, 255);
   analogWrite(pwm4, 255);
   
   digitalWrite(i1,HIGH);
   digitalWrite(i3,HIGH);
   digitalWrite(i5,HIGH);
   digitalWrite(i7,HIGH);
   digitalWrite(i2,LOW);
   digitalWrite(i4,LOW);
   digitalWrite(i6,LOW);
   digitalWrite(i8,LOW);
   delay(400);   
 }

else {
  // Climb when Button activates and even
   analogWrite(pwm1, 255);
   analogWrite(pwm2, 255);
   analogWrite(pwm3, 255);
   analogWrite(pwm4, 255);
   
   digitalWrite(i2,HIGH);
   digitalWrite(i4,HIGH);
   digitalWrite(i6,HIGH);
   digitalWrite(i8,HIGH);
   digitalWrite(i1,LOW);
   digitalWrite(i3,LOW);
   digitalWrite(i5,LOW);
   digitalWrite(i7,LOW);
   delay(400);
}
}
  delay(1000);
  ...
  delay(400);
  ...
  delay(400);

These delays are part of the problem.

There must be a zillion examples out there which show you how to detect that a button has become pressed instead of just simply detecting that it is pressed.

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

 if (state = HIGH){

That sets the value of 'state' to HIGH. You probably wanted to TEST the value of state:

 if (state == HIGH){

You are not keeping track of the previous value of the button so the direction will toggle every time you check the button. You need to add an "if (state != previousState)" in there somewhere before you process the button press.

johnwasser: if (state = HIGH){

That sets the value of 'state' to HIGH. You probably wanted to TEST the value of state:

 if (state == HIGH){

You are not keeping track of the previous value of the button so the direction will toggle every time you check the button. You need to add an "if (state != previousState)" in there somewhere before you process the button press.

Thanks all!

helped a lot!

My motors are not moving back and forth but still stops moving When I disconnect the pin 13 (pushbutton).

Your initial mode is stop I assume. Maybe it would be better if..

first press move forward second press stop third press reverse fourth press back to initial mode witch is stop

It is just a suggestion. Tell us what you think about it.

Gabriel_swe: Your initial mode is stop I assume. Maybe it would be better if..

first press move forward second press stop third press reverse fourth press back to initial mode witch is stop

It is just a suggestion. Tell us what you think about it.

Can We make like

initial -Stop Press Button -Fwd Press Button again -Reverse

Still figuring out how to make the motor keep moving even if I take out the button wire...

jaebungs: Still figuring out how to make the motor keep moving even if I take out the button wire...

That was answered in Reply #1. And again by me in Reply #3:

johnwasser: You are not keeping track of the previous value of the button so the direction will toggle every time you check the button. You need to add an "if (state != previousState)" in there somewhere before you process the button press.

jaebungs: Can We make like

initial -Stop Press Button -Fwd Press Button again -Reverse

Still figuring out how to make the motor keep moving even if I take out the button wire...

And third press is stop? I find it a little bit weird to slam into reverse directly from forward before you make a stop.

That's how it works in rental cars right?