Running a Stepper Motor using enable input

I am running a stepper motor at different speeds using the following code :

#include <AccelStepper.h>

AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

// set pin numbers:
const int buttonPin6 = 11;     // the number of the pushbutton pin
const int buttonPin5 = 10;     // the number of the pushbutton pin
const int buttonPin4 = 9;     // the number of the pushbutton pin
const int buttonPin3 = 8;     // the number of the pushbutton pin
const int buttonPin2 = 7;     // the number of the pushbutton pin
const int buttonPin1 = 6;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int c1,c2,c3,c4,c5,c6=0;
// variables will change:
int buttonState1,buttonState2,buttonState3,buttonState4,buttonState5,buttonState6 = 0;         // variable for reading the pushbutton status

void setup() {

  stepper.setMaxSpeed(360);//max speed at 3 rpm
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
  stepper.setSpeed(0.333333);  // 20 mm/hr
  c1=1;}
  if (buttonState2 == HIGH) {
  stepper.setSpeed(1);  // 60 mm/hr
  c2=1;}
    if (buttonState3 == HIGH) {
  stepper.setSpeed(2);  // 120 mm/hr
  c3=1;}
    if (buttonState4 == HIGH) {
  stepper.setSpeed(4); // 240 mm/hr
  c4=1;}
    if (buttonState5 == HIGH) {
  stepper.setSpeed(8);  // 480 mm/hr
  c5=1;}
    if (buttonState6 == HIGH) {
  stepper.setSpeed(16);  // 960 mm/hr
  c6=1;}
  if(c1+c2+c3+c4+c5+c6==1){
  stepper.runSpeed();
  }
  c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;
}

This works perfectly and I am using it regularly.

Now i want to modify the code such that when pin 12 goes high, then only the above code should work or else motor should stop.

I have done the following code, but the motor doesnt run.

#include <AccelStepper.h>

AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

// set pin numbers:
const int buttonPin6 = 11;     // the number of the pushbutton pin
const int buttonPin5 = 10;     // the number of the pushbutton pin
const int buttonPin4 = 9;     // the number of the pushbutton pin
const int buttonPin3 = 8;     // the number of the pushbutton pin
const int buttonPin2 = 7;     // the number of the pushbutton pin
const int buttonPin1 = 6;     // the number of the pushbutton pin
const int ePin =  12;      // the enable pin to run further code
int c1,c2,c3,c4,c5,c6=0;
// variables will change:
int buttonState1,buttonState2,buttonState3,buttonState4,buttonState5,buttonState6,ePinstate = 0;         // variable for reading the pushbutton status

void setup() {

  stepper.setMaxSpeed(360);//max speed at 3 rpm
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(ePin, INPUT);
}

void loop() {
  ePinstate = digitalRead(ePin);
  if (ePin == HIGH)
  {
                      // read the state of the pushbutton value:
                      buttonState1 = digitalRead(buttonPin1);
                      buttonState2 = digitalRead(buttonPin2);
                      buttonState3 = digitalRead(buttonPin3);
                      buttonState4 = digitalRead(buttonPin4);
                      buttonState5 = digitalRead(buttonPin5);
                      buttonState6 = digitalRead(buttonPin6);
                    
                      // check if the pushbutton is pressed.
                      // if it is, the buttonState is HIGH:
                      if (buttonState1 == HIGH) {
                      stepper.setSpeed(0.333333);  // 20 mm/hr
                      c1=1;}
                      if (buttonState2 == HIGH) {
                      stepper.setSpeed(1);  // 60 mm/hr
                      c2=1;}
                        if (buttonState3 == HIGH) {
                      stepper.setSpeed(2);  // 120 mm/hr
                      c3=1;}
                        if (buttonState4 == HIGH) {
                      stepper.setSpeed(4); // 240 mm/hr
                      c4=1;}
                        if (buttonState5 == HIGH) {
                      stepper.setSpeed(8);  // 480 mm/hr
                      c5=1;}
                        if (buttonState6 == HIGH) {
                      stepper.setSpeed(120);  // 960 mm/hr
                      c6=1;}
                      if(c1+c2+c3+c4+c5+c6==1){
                      stepper.runSpeed();
                      }
                      c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;
  }
  else
  {
    stepper.setSpeed(0);
    stepper.runSpeed();
  }
}

I am running a chart drive. The speeds moves the chart by specified distance.
Can anyone rectify my mistake and tell me a suitable solution. Thank You !!!

siren215:
Now i want to modify the code such that when pin 12 goes high, then only the above code should work or else

I think if you take your original code and add a little to it like this, it should do what you want

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
  ePinstate = digitalRead(ePin);   //  <----------NEW


  //  SNIP
  
  
  if (ePin == LOW) {           //  <----------NEW
     stepper.setSpeed(0);      //  <----------NEW
  }                            //  <----------NEW
  if(c1+c2+c3+c4+c5+c6==1){
  stepper.runSpeed();
  }
  c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;
}

…R

As usual the motor keeps on spinning on the code which you had sent. I added the following if condition in the end to see if it works, but failed.

 if (ePin == LOW) {           //  <----------NEW
                      stepper.setSpeed(0);      //  <----------NEW
                      e1=0;}                            //  <----------NEW
                      
                      if(c1+c2+c3+c4+c5+c6==1 && e1==0)  {
                      stepper.runSpeed();
                      }

siren215:
As usual the motor keeps on spinning on the code which you had sent. I added the following if condition in the end to see if it works, but failed.

I had this line wrong (sorry)
if (ePin == LOW) { // <----------NEW
it should be
if (ePinstate == LOW) { // <----------NEW

(The usual “deliberate error” to see if the reader is awake :slight_smile: )

…R

Working on the test unit from UNO. Will check on my final unit which is on NANO. I was thinking of some complex method to achieve this whereas this method which you told is pretty simple. Thanks a lot. Cheers!