Pushing Button Once and Having Stepper Motor Cycle Through Comands

Hey guys. I am working on programming an arduino uno with a stepper motor. It will act as a conveyor where it rotates one way for a set distance, wait, than return to its original position. I would like to have the loop hooked up to a button so when the button is pressed the conveyor will go down and back. I have seen a lot of programs where the button has to be held down, but I haven't seen anything that allows you to push the button once and have the conveyor cycle through the loops. Any suggestions? I have included my code below. Thanks!

void setup() {
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
Serial.begin(9600);
}

void loop() {

int n=1;

while (n<=400){
Serial.println(n);

digitalWrite(8, HIGH);
delayMicroseconds(4200);
digitalWrite(8, LOW);
delayMicroseconds(1200);
n=n+1;
}
delay(2000);
int j=1;
while (j<=200){
Serial.println(j);

digitalWrite(7, HIGH);
delayMicroseconds(4200);
digitalWrite(7, LOW);
delayMicroseconds(4200);
j=j+1;
}

while(1){}
}

Put your code in code tags (</> button).

Also this is never a good idea:

while(1){}

Stepper motors usually have 4 or 6 wires, and it looks like you are only using 2 pins. Are you using some kind of stepper motor driver?

Also, be careful about plugging your stepper motor wires directly into the arduino. Stepper motors typically need a lot of current to run and your arduino may not be able to provide enough for it to turn

Yes, I am using an ST10-S Step Motor Driver. The while(1) was something quick I found online to use while testing the motor. I would like to be able to get out of the void loop once the conveyor goes down and back I just haven't got that far. I could use some suggestions for that as well if anyone has some.

[/void setup() {
  pinMode(8, OUTPUT);  
  pinMode(7, OUTPUT);  
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  Serial.begin(9600);
}

void loop() {

  int n=1;
  
  while (n<=400){
  Serial.println(n);

  digitalWrite(8, HIGH);
  delayMicroseconds(4200);
  digitalWrite(8, LOW);
  delayMicroseconds(1200);
  n=n+1;
  }
delay(2000);
  int j=1;
  while (j<=200){
  Serial.println(j);
 
  digitalWrite(7, HIGH);
  delayMicroseconds(4200);
  digitalWrite(7, LOW);
  delayMicroseconds(4200);
  j=j+1;
  }
  
  while(1){}
}]

Use a finite state machine, pseudocode:

enum state_e
{
	WAIT_FOR_BUTTON = 0,
	STEPPER_FORWARD, 
	STEPPER_REVERSE,
};

state_e g_state = WAIT_FOR_BUTTON;

void loop()
{
	switch( g_state )
	{
		case WAIT_FOR_BUTTON:
		{
			if( button_pressed ) // digitalRead ...
			{
				g_state = STEPPER_FORWARD;
			}
		}
		break;

		case STEPPER_FORWARD:
		{
			// do stepper forward code
			if( finished_forward_part )
			{
				g_state = STEPPER_REVERSE;
			}
		}
		break;

		case STEPPER_REVERSE:
		{
			// do stepper reverse routine
			if( finished_reverse_part )
			{
				g_state = WAIT_FOR_BUTTON;	// go back to waiting for input.
			}
		}
		break;
	}
}

Perhaps something like this

void loop() {
   readButton();
   checkMotor();
   motorMove();
}

void readButton() {
  // code to read the button and save its value
}

void checkMotor() {
 if (motorStopped == true) {
    if (buttonPressed == true) {
       if (motorDir == 'L') {
          motorDir = 'R';
       }
       else {
          motorDir = 'L';
       }
       motorStopped = false;
    }
  }
}

void motorMove(char dirn) {
    if (motorStopped = false) {
      motorPos = 0;
      if (motorDir == 'R') {
        // set direction
      }
      else {
        // set other direction
      }
      if (timeForNextStep) {   // pseudo code to represent checking if a step is due
         motorStep();               // function to make the motor move 1 step
         motorPos ++;
      }
      if (motorPos >= motorLimit) {
         motorStopped = true;
      }
}

The variables motorStopped and motorDir represent the different states of the system

...R
Stepper Motor Basics
Planning and Implementing a Program

I will work with both of these and see if I can get something to work. Thanks for the help!