Help me to add start and stop button to below sketch.

I have this working code for Arduino no. All goes well. Now I want to add buttons to start and stop the Sketch.
When I power on the board, It should start if I press the Start button, else it should be in an idle state.
After the start button pressed it should function endless, till stop button pressed.

Thanks in Advance.

const int stepPin = 9; 
const int dirPin = 8; 

#define Pin1 2  //Relay 1 #1
#define Pin2 3  //Relay 2  #2
#define Pin3 4  // Relay 3  #3
#define Pin4 13  // Relay 4  #3

#define home_switch 12 // Pin 12 connected to Home Switch (MicroSwitch)
int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

void setup() {
 // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  pinMode(Pin4,OUTPUT);

 
  digitalWrite(Pin1,LOW);
  digitalWrite(Pin2,LOW);
  digitalWrite(Pin3,LOW);
  digitalWrite(Pin4,LOW);
  
  pinMode(home_switch, INPUT_PULLUP);
  digitalWrite(stepPin, HIGH);  // Wake up EasyDriver
   delay(20);  // Wait for EasyDriver wake up

// Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(dirPin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(stepPin, HIGH);
    delay(25);                       // Delay to slow down speed of Stepper
    digitalWrite(stepPin, LOW);
    delay(25);   
}
  steps=0;  // Reset position variable to zero 
}
void loop() {

 {digitalWrite(Pin4,HIGH);
  delay(300);
  digitalWrite(Pin4,LOW);
  delay(200);}
  
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(600); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(600); 
  }
  {
  digitalWrite(Pin1,HIGH);
  delay(200);
  digitalWrite(Pin1,LOW);
  delay(200);}
  {digitalWrite(Pin2,HIGH);
  delay(600);
  digitalWrite(Pin2,LOW);
  delay(5);}
  {digitalWrite(Pin3,HIGH);
  delay(300);
  digitalWrite(Pin3,LOW);
  delay(200);}
  
}

I would suggest looking into interrupts. Create a boolean called "running" that is set to true when you hit the "go" button, and false when you hit the "stop" button. Then in the loop function, just do "while(!running) {}".

Normally I would recommend just doing it normally with digitalRead, but since the whole loop can take some time to function by the looks of it, this solution wouldn't work well in your case.

Thanks for the suggestion, it seems pretty easy for you, not for me. I am noob in figuring it out how. Please donate your precious time to my code.

Due to physical hardware limitations, I’ve had to shift your relay pins over a bit, but it should work with your buttons now. The uno only accepts pins 2 and 3 as interrupt pins:

const int stepPin = 9;
const int dirPin = 8;

#define Run_Button 2
#define Stop_Button 3
#define Pin1 4  //Relay 1 #1
#define Pin2 5  //Relay 2  #2
#define Pin3 6  // Relay 3  #3
#define Pin4 13  // Relay 4  #3

#define home_switch 12 // Pin 12 connected to Home Switch (MicroSwitch)
int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

bool running = true;

void setup() {
  attachInterrupt(digitalPinToInterrupt(Run_Button),setGo,RISING);
  attachInterrupt(digitalPinToInterrupt(Stop_Button),setStop,RISING);
  
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  pinMode(Pin4,OUTPUT);

 
  digitalWrite(Pin1,LOW);
  digitalWrite(Pin2,LOW);
  digitalWrite(Pin3,LOW);
  digitalWrite(Pin4,LOW);
 
  pinMode(home_switch, INPUT_PULLUP);
  digitalWrite(stepPin, HIGH);  // Wake up EasyDriver
  delay(20);  // Wait for EasyDriver wake up

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(dirPin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(stepPin, HIGH);
    delay(25);                       // Delay to slow down speed of Stepper
    digitalWrite(stepPin, LOW);
    delay(25);   
  }
  steps=0;  // Reset position variable to zero
}

void loop() {
  while (!running) {}
  
  digitalWrite(Pin4,HIGH);
  delay(300);
  digitalWrite(Pin4,LOW);
  delay(200);
 
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(600);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(600);
  }
  
  digitalWrite(Pin1,HIGH);
  delay(200);
  digitalWrite(Pin1,LOW);
  delay(200);
  digitalWrite(Pin2,HIGH);
  delay(600);
  digitalWrite(Pin2,LOW);
  delay(5);
  digitalWrite(Pin3,HIGH);
  delay(300);
  digitalWrite(Pin3,LOW);
  delay(200);
}

void setGo() { running = true; }
void setStop() { running = false; }

Hi virencq,

would it be OK if the real stopping would happen after finishing the last started action or do you really need an instantly stop regardless of what the program is doing?

If you need an immediate stop the interrupt-solution does hwo it is named interrupting immediately.
using interrupts is a medium advanced technique in coding. not the third thing a newbee does.

If you write something about the complete project a good suiting solution can be suggested.
f.e. there are quite easy to use code-functions that deliver controlling a stepper-motor with functions like move x steps into direction, move from position x1 to position x2 etc. and you don't have to care about how many pulses must be created to do that. While the arduino is doing the motor-move your code can do other things in parallel.

If this is a good solution or not depends on what you want to to in the end. Moving a steppermotor is not the final purpose.

If you need this project up and runnig ASAP and it is sufficient to start/stop the motor by pressing buttons
the above solution is best. If you have time to dive into more advanced motor-control and read

something like using the AccelStepper-library might be a better one.

best regards

Stefan

Technokid2000:
I would suggest looking into interrupts. Create a boolean called "running" that is set to true when you hit the "go" button, and false when you hit the "stop" button. Then in the loop function, just do "while(!running) {}".

Normally I would recommend just doing it normally with digitalRead, but since the whole loop can take some time to function by the looks of it, this solution wouldn't work well in your case.

I don't know why is this not working in my sketch.
It starts automatically and by pressing the stop button, nothing happens.

#define STEP 9
#define DIR 8
#define Pin1 4  // Relay 1  #1
#define Pin2 5  // Relay 2  #2
#define Pin3 6  // Relay 3  #3

#define home_switch 12 // Pin 12 connected to Home Switch (MicroSwitch)
int direction;         // Variable to set Rotation (CW-CCW) of the motor
int steps;             // Used to set HOME position after Homing is completed
byte processState;  // 0 is OFF, 1 is ON
byte pin2prevState;
byte pin3prevState;
byte readState;
void setup() {
  
  // Sets the two pins as Outputs
  pinMode(STEP,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(Pin1,OUTPUT);
  pinMode(Pin2,OUTPUT);
  pinMode(Pin3,OUTPUT);
  digitalWrite(Pin1,LOW);
  digitalWrite(Pin2,LOW);
  digitalWrite(Pin3,LOW);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(home_switch, INPUT_PULLUP);
  digitalWrite(STEP, HIGH);  // Wake up EasyDriver
  digitalWrite(DIR,HIGH);
  delay(10);  // Wait for EasyDriver wake up

  // Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(DIR, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(STEP, HIGH);
    delay(5);                       // Delay to slow down speed of Stepper
    digitalWrite(STEP, LOW);
    delay(5);   
  }
  steps=10;  // Reset position variable to zero
}
 
void loop() {
readState = digitalRead( 2 );
 if ( pin2prevState != readState )
 {
  pin2prevState = readState;
  if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
  {
   processState = 1;
  }
 }
 readState = digitalRead( 3 ); // note that the stop button is read last. press both makes stop
 if ( pin3prevState != readState )
 {
  pin3prevState = readState;
  if ( readState == LOW ) // button down with INPUT_PULLUP drains the pin to ground.
  {
   processState = 0;
  }
 }
 if ( processState )
 {
digitalWrite(DIR,LOW); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 100; x++) {
    digitalWrite(STEP,HIGH);
    delayMicroseconds(1000);
    digitalWrite(STEP,LOW);
    delayMicroseconds(1000);  
  }
  {
  digitalWrite(Pin1,HIGH); // locker & cutter
  delay(300);
  digitalWrite(Pin1,LOW);
  delay(300);
  digitalWrite(Pin2,HIGH); // punch
  delay(600);
  digitalWrite(Pin3,HIGH); // release
  delay(200);
  digitalWrite(Pin2,LOW);
  delay(200);
  digitalWrite(Pin3,LOW); 
  delay(300);
  }
 }
 else
 {
  // turn the motor and led off
  digitalWrite(STEP,LOW);
    delayMicroseconds(1000);  
 }
}

I managed to achieve my requirements from the code above.
Now my sketch start and stop with button.
A big thanks to community…