Controlling a DC motor - loop only runs once?

Hi all,

I am trying to control a DC motor with Arduino via a L298N bridge. The idea is to run the motor in one direction for 3 times, with pauses in between. The duration of each run is the same and is specified as "forward", while the duration of the pause being "pause". Afterwards, the motor would be driven in the reverse direction, with the duration of the run given by "backward". This whole process is started by a push button and should keep repeating itself once it has started.

Here is the code that I have written. It ran fine for one time (i.e., 3 forward runs + 1 backward run) and then stopped and failed to repeat the steps. I am hoping to get your advice on how to modify the code such that:

Aim 1) the steps can keep looping

Aim 2) the steps can keep looping until a second push of the button, at which point everything should be reset. The program can then be reinitiated.

Thank you so much!

WY

#define enA 9
#define in1 6
#define in2 7
#define buttonPin 2

int forPwmOutput;
int backPwmOutput;
int pause;
int forward;
int backward;
bool buttonState;
unsigned long StartMillis;

void setup() {
  pinMode(enA, OUTPUT); // To L298N Enable Pin
  pinMode(in1, OUTPUT); // To L298N IN1
  pinMode(in2, OUTPUT); // To L298N IN2
  pinMode(buttonPin, INPUT); // From Button
}

void initialize() {
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // Set rotation speed
  forPwmOutput = 255;
  backPwmOutput = 255;
  // Set amount of time for each action
  pause = 2000;
  forward = 2000;
  backward = 4000;
  // Set initial state of the button
  buttonState = false;
  StartMillis = 0;
}


void loop() {

  while(buttonState == false) {
    if (digitalRead(buttonPin) == HIGH) {
    buttonState = true;
  }
  }
 
  if ((buttonState == true) && (StartMillis = 0)) {
    StartMillis = millis();
  }

  if (buttonState == true) {
    // Moving to position 2
    if (((millis() - StartMillis) >= pause) && ((millis() - StartMillis) < (pause + forward))) {
      analogWrite(enA, forPwmOutput);   
    }
    // Videotaping at position 2
    if (((millis() - StartMillis) >= (pause + forward)) && ((millis() - StartMillis) < (2*pause + forward))) {
      analogWrite(enA, 0);    
    }
    // Moving to position 3
    if (((millis() - StartMillis) >= (2*pause + forward)) && ((millis() - StartMillis) < (2*pause + 2*forward))) {
      analogWrite(enA, forPwmOutput);
    }
    // Videotaping at position 3
    if (((millis() - StartMillis) >= (2*pause + 2*forward)) && ((millis() - StartMillis) < (3*pause + 2*forward))) {
      analogWrite(enA, 0);
    }
    // Moving to position 4
    if (((millis() - StartMillis) >= (3*pause + 2*forward)) && ((millis() - StartMillis) < (3*pause + 3*forward))) {
      analogWrite(enA, forPwmOutput);
    }
    // Videotaping at position 4
    if (((millis() - StartMillis) >= (3*pause + 3*forward)) && ((millis() - StartMillis) < (4*pause + 3*forward))) {
      analogWrite(enA, 0);
    }
    // Returning to position 1
    if (((millis() - StartMillis) >= (4*pause + 3*forward)) && ((millis() - StartMillis) < (4*pause + 3*forward + backward))) {
      digitalWrite(in1, HIGH); // Reverse rotation direction
      digitalWrite(in2, LOW);
      analogWrite(enA,backPwmOutput);
    }
    // At position 1
    if ((millis() - StartMillis) >= (4*pause + 3*forward + backward)) {
      analogWrite(enA, 0);
      initialize();
      buttonState = true;
    }
  }
}

When your "buttonState" becomes "true", it never returns to "false" again.. Typo in the last code line, perhaps?

EDIT:

if ((buttonState == true) && (StartMillis = 0)) { //Should be "StartMillis == 0"
    StartMillis = millis();
  }

Good catch. That's a simple solution to the problem. Thanks!