How do I pause/halt a sketch?

This is my first Arduino project, so please excuse my lack of knowledge. The following sketch runs a microprocessor controlled stepper motor , therefore using only 2 outputs from the UNO, and a small solid state relay to open dispensing valves. My question is, how can I halt the program loop and then continue the sketch after using the "reset" or other input.

#include <Stepper.h>
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(1000, 8, 9);
void setup() {
stepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
// initialize the digital pins as an output.
// LED On 13 and SSR 7 relay output
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
//set ST17 back to position P20
pinMode(2, OUTPUT);
}
void loop() {
//press reset button to start sequence?????????????????????
// reset motor to home position of P20 using Evodrive ST-17
digitalWrite(2, HIGH);
delay(200);
digitalWrite(2, LOW);
delay(5000); // wait for home
Serial.println("clockwise");
//move to tube 1
stepper.step(1000);
// turn on LED and SSR relay output
// relay "On" ,Solid State Relay triggered for dispense
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//move to next tube
stepper.step(1000);
// turn on LED and SSR relay output
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);
//dispense time milliseconds
delay(5000);
digitalWrite(7, LOW);
digitalWrite(13, LOW);
delay(1000);
//return tray to home position
Serial.println("counterclockwise");
stepper.step(-10000);

}

Easiest way I can think of is wrapping all the stuff in the loop() with an if statement, which checks some button condition? If the button is pressed/has been pressed, bypass the loop?

one way of doing this, would be using functions.

attach a button to your setup and add a variable to track what your programme is doing.

write a function with no return type, which requires no parameters.

so something like this: (dont copy paste.. this wont work)

int counter = 1;

void loop()  {

      if (button pressed) {
       counter = counter * -1;
      }
   
      while (counter == 1) {
      yourFunction();
       } 

}
void yourFunction() {
your program here.
}

EDIT: yeah. or just use a simple iff statement, as the person above me points out. I am thinking a bit complicated here, though my method might make your code easyer to read... post above mine was posted while i was writing mine...

Thanks tomm and fkeel. I really apreciate your help.

Oooh please put that lot into a loop, it makes my brain hurt just looking at it :slight_smile:

Here's a version that will run when a button is pressed,

void loop() {
 

  if (digitalRead(buttonPin) == HIGH)  {
	 //press reset button to start sequence?????????????????????
	  // reset motor to home position of P20 using Evodrive ST-17
	  digitalWrite(2, HIGH);
	  delay(200);
	  digitalWrite(2, LOW);
	  delay(5000); // wait for home
	  Serial.println("clockwise");  
	  for (int i = 0; i < 9; i++) {
		  
			  //move to next tube
			  stepper.step(1000);
			  // turn on LED and SSR relay output
			  // relay "On" ,Solid State Relay triggered for dispense
			  digitalWrite(13, HIGH);
			  digitalWrite(7, HIGH);
			  //dispense time milliseconds
			  delay(5000);
			  digitalWrite(7, LOW); 
			  digitalWrite(13, LOW);
			  delay(1000);
		}
		 
		 //return tray to home position
		  Serial.println("counterclockwise");
		  stepper.step(-10000);
	}
}

Rob

Or the much more logical (pun intended) version with booleans rather than multiplying by minus one:

boolean active_state = true ;

void loop()  {

  if (button pressed) {
    active_state = !active_state ;  // toggle active_state
    delay (10) ; // debounce
  }
   
  if (active_state) { // if not while so the button gets detected next time round
    yourFunction();
} 

}

Thanks, that worked really well.