Program Only Runs Once, Requires Restart to Run Again

Hi,

I hope this is the correct section for this post.
I have an Uno R3 hooked up to a TB660 stepper driver. I have 4 buttons going into the Uno. Depending on which button is pressed, the stepper turns a set number of times. 2 of the buttons are just a simple forward and reverse jog. The other 2 are much longer 'for' loops.

All of this is working, however, after pressing the button for one of the longer loops, the loops executes but then will not take any more button presses. If I disconnect the power, everything works again.
This does not happen every time, some of the time it completes the loop and then will jog or run one of the longer loops again if the buttons are pressed.

Is there something in my code that is preventing the system from continuing to monitor the buttons for presses?

My Code:

void setup() 
{
  pinMode(8, OUTPUT);       // FORWARD(LEFT) = LOW; REVERSE(RIGHT) = HIGH 
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  pinMode(2, INPUT_PULLUP); // BUTTON2 - 5GAL HEAD 
  pinMode(3, INPUT_PULLUP); // BUTTON3 - 15GAL HEAD 
  pinMode(4, INPUT_PULLUP); // BUTTON4 - LEFT JOG 
  pinMode(5, INPUT_PULLUP); // BUTTON5 - RIGHT JOG 
}

void loop() 
{
  
  if (digitalRead(2) == LOW)                    // Button 2 PRESSED - 5GAL 
  {
    for (int loop=0; loop<4000; loop++)          // SET NUMBER OF LOOPS!!!!!   1000 = ~1"
    {  
      digitalWrite(8, LOW);                    // TURN COUNTERCLOCKWISE - CUTTING
      digitalWrite(9, HIGH);
      delay(50);                               // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                              // ADJUST SPEED
    }
    
    delay(10000);                                // PAUSE TO FINALLIZE CUT
    
    for (int loop=0; loop<4000; loop++)          // SET NUMBER OF LOOPS!!!!!
    {  
      digitalWrite(8, HIGH);                     // TURN CLOCKWISE - RETREATING
      digitalWrite(9, HIGH);
      delay(25);                                 // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                 // ADJUST SPEED
    }
  }
  
  if (digitalRead(3) == LOW)                    // Button 3 PRESSED - 15GAL
  {
    for (int loop=0; loop<3000; loop++)          // SET NUMBER OF LOOPS!!!!!     1000 = ~1"
    {  
      digitalWrite(8, LOW);                    // TURN COUNTERCLOCKWISE - CUTTING
      digitalWrite(9, HIGH);
      delay(50);                                // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                // ADJUST SPEED
    }
    
    delay(10000);                                // PAUSE TO FINALLIZE CUT
    
    for (int loop=0; loop<3000; loop++)          // SET NUMBER OF LOOPS!!!!!
    {  
      digitalWrite(8, HIGH);                     // TURN CLOCKWISE - RETREATING
      digitalWrite(9, HIGH);
      delay(25);                                 // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                 // ADJUST SPEED
    }
  }
  
  if (digitalRead(4) == LOW)   // BUTTON 4 PRESSED - CLOCKWISE - LEFT ONCE
  { 
    digitalWrite(8, LOW);                      // FORWARD - CUTTING
    digitalWrite(9, HIGH);
    delay(10);                                   // ADJUST SPEED
    digitalWrite(9, LOW);
    delay(1);                                   // ADJUST SPEED
  }
  
  if (digitalRead(5) == LOW)  // BUTTON 5 PRESSED - COUNTER-CLOCKWISE - RIGHT ONCE
  {
    digitalWrite(8, HIGH);                       // REVERSE - RETREATING
    digitalWrite(9, HIGH);
    delay(10);                                   // ADJUST SPEED
    digitalWrite(9, LOW);
    delay(1);                                   // ADJUST SPEED
  }
}

Is there something in my code that is preventing the system from continuing to monitor the buttons for presses?

  delay(10000);

Also, your loops are asymmetric (which is not really a problem, just poor form) and the dir pin setting should be removed to just before the loop.

I am waiting until after the delay and the second half of the movement before trying another button.

I will try moving my dir to outside the loop

Each of your long for loops also means you miss key presses.

I am okay with missing the key presses when the long loops are running. However, after the long loop runs sometimes it is like the whole thing goes dead. It is completely unresponsive.

I want to be able to run a long loop and then when it finishes take a real world action and then run the loop again. Sometimes this is possible other times it is not. I do not understand why it works one time but not another.

So I rewrote my code to put the dir outside the loop.

Now is there anything that would explain not being able to push another button after one of the longer loops completes?

void setup() 
{
  pinMode(8, OUTPUT);       // FORWARD(LEFT) = LOW; REVERSE(RIGHT) = HIGH 
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  pinMode(2, INPUT_PULLUP); // BUTTON2 - 5GAL HEAD 
  pinMode(3, INPUT_PULLUP); // BUTTON3 - 15GAL HEAD 
  pinMode(4, INPUT_PULLUP); // BUTTON4 - LEFT JOG 
  pinMode(5, INPUT_PULLUP); // BUTTON5 - RIGHT JOG 
}

void loop() 
{
  
  if (digitalRead(2) == LOW)                    // Button 2 PRESSED - 5GAL 
  {
    digitalWrite(8, LOW);                       // TURN COUNTERCLOCKWISE - CUTTING
    for (int loop=0; loop<4000; loop++)         // SET NUMBER OF LOOPS!!!!!   1000 = ~1"
    {  
      digitalWrite(9, HIGH);
      delay(50);                                // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                 // ADJUST SPEED
    }
    
    delay(10000);                                // PAUSE TO FINALLIZE CUT
    digitalWrite(8, HIGH);                       // TURN CLOCKWISE - RETREATING
    for (int loop=0; loop<4000; loop++)          // SET NUMBER OF LOOPS!!!!!
    {  
      digitalWrite(9, HIGH);
      delay(25);                                 // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                  // ADJUST SPEED
    }
  }
  
  if (digitalRead(3) == LOW)                    // Button 3 PRESSED - 15GAL
  {
    digitalWrite(8, LOW);                        // TURN COUNTERCLOCKWISE - CUTTING
    for (int loop=0; loop<3000; loop++)          // SET NUMBER OF LOOPS!!!!!     1000 = ~1"
    {  
      digitalWrite(9, HIGH);
      delay(50);                                  // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                   // ADJUST SPEED
    }
    
    delay(10000);                                // PAUSE TO FINALLIZE CUT
    digitalWrite(8, HIGH);                       // TURN CLOCKWISE - RETREATING
    for (int loop=0; loop<3000; loop++)          // SET NUMBER OF LOOPS!!!!!
    {  
      digitalWrite(9, HIGH);
      delay(25);                                 // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                 // ADJUST SPEED
    }
  }
  
  if (digitalRead(4) == LOW)   // BUTTON 4 PRESSED - CLOCKWISE - LEFT ONCE
  { 
    digitalWrite(8, LOW);                        // FORWARD - CUTTING
    digitalWrite(9, HIGH);
    delay(10);                                   // ADJUST SPEED
    digitalWrite(9, LOW);
    delay(1);                                   // ADJUST SPEED
  }
  
  if (digitalRead(5) == LOW)  // BUTTON 5 PRESSED - COUNTER-CLOCKWISE - RIGHT ONCE
  {
    digitalWrite(8, HIGH);                       // REVERSE - RETREATING
    digitalWrite(9, HIGH);
    delay(10);                                   // ADJUST SPEED
    digitalWrite(9, LOW);
    delay(1);                                   // ADJUST SPEED
  }
}
for (int loop=0; loop<4000; loop++)         // SET NUMBER OF LOOPS!!!!!   1000 = ~1"
    { 
      digitalWrite(9, HIGH);
      delay(50);                                // ADJUST SPEED
      digitalWrite(9, LOW);
      delay(1);                                 // ADJUST SPEED
    }

4000 x 51 = nearly three and a half minutes.

I understand that the buttons are not be monitored while the loops are running for that ~5 minutes. I have no reason to press the buttons while the loop is executing. I do not want to press another button until it completes.

The stepper is operating a ball screw and I watch it move through the whole forward loop and then pause and then through the whole reverse loop. Once the all that completes, I am expecting it to go back to a state of waiting for a button press.

Am I correct in understanding that after the loop has concluded, that the buttons should once again be monitored?
Should it then just wait indefinitely for a button to be pressed?
Is my code written correctly to have it wait for future button presses after it completes the loops associated with a button press?

So just as a followup, I figured out what was causing my issue. It was not the program at all. For some reason the TB6600 stepper driver is losing power. I do not understand why it is happening but it powering down is what I was interpreting as the program not taking any more button presses.

Thank you for the assistance. It may not have solve my issue but I did make some improvements to my code as a result.