Stepper Motor Button Code

Hey everyone, I have the code below which is used to turn a stepper motor in forward or reverse a certain number of steps depending on which of the two buttons is pushed. To activate forward or reverse, the button must be pressed and released. Is there a simple way to change this code so that the motor will continue to turn in forward or reverse for as long as the button is pressed? This is opposed to just turned the defined number of steps per the code. If anyone has any similar code to reference that would be much appreciated. Thanks!

// defines pins numbers

const int dirPin  = 3;

const int stepPin = 4;

const int enPin   = 5;



const int switchOne     = 8;

const int switchTwo     = 9;



int p1buttonState = 0;         // current state of the button

int lastp1buttonState = 0;     // previous state of the button



int p2buttonState = 0;         // current state of the button

int lastp2buttonState = 0;     // previous state of the button

bool bPress = false;



bool isForward = false;

bool isBackward = false;



void setup() {



  Serial.begin(9600);

  pinMode( switchOne, INPUT_PULLUP);

  pinMode( switchTwo, INPUT_PULLUP);



  // Sets the two pins as Outputs

  pinMode(stepPin, OUTPUT);

  pinMode(dirPin, OUTPUT);



  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);



}

void loop() {



  isForward = false;

  isBackward = false;



  p1buttonState = digitalRead(switchOne);

  p2buttonState = digitalRead(switchTwo);



  if (p1ButtonPress()) {



    digitalWrite(dirPin, HIGH);



    delay(5);

  }



  if (p2ButtonPress()) {



    digitalWrite(dirPin, LOW);



    delay(5);

  }



  if ( isForward || isBackward ) {



    for (int x = 0; x < 2000; x++) { //2000 = 360 degrees at 400pulse/rev

      digitalWrite(stepPin, HIGH);

      delayMicroseconds(500); //sets speed

      digitalWrite(stepPin, LOW);

      delayMicroseconds(500); //sets speed

    }

  }



}



bool p1ButtonPress()

{

  bool isPress = false;

  // compare the p1buttonState to its previous state

  if (p1buttonState != lastp1buttonState) {

    // if the state has changed, increment the counter

    if (p1buttonState == LOW) {

      // if the current state is HIGH then the button went from off to on:

      bPress = true;

      isPress = true;

      Serial.println("Plaer One score");



    } else {

      // if the current state is LOW then the button went from on to off:

      Serial.println("off");

      isForward = true;

    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  lastp1buttonState = p1buttonState;

  return isPress;

}



bool p2ButtonPress()

{

  bool isPress = false;

  // compare the p1buttonState to its previous state

  if (p2buttonState != lastp2buttonState) {

    // if the state has changed, increment the counter

    if (p2buttonState == LOW) {

      // if the current state is HIGH then the button went from off to on:

      bPress = true;

      isPress = true;

      Serial.println("Plaer Two score");



    } else {

      // if the current state is LOW then the button went from on to off:

      Serial.println("off");

      isBackward = true;

    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  lastp2buttonState = p2buttonState;

  return isPress;

}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch. Use the </> icon from the ‘posting menu’ to attach the copied sketch.

Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring. Give links to components.

You mean while the switch is pressed ?

Yes, use the Bounce2 library which does all the work to handle your buttons (there are examples). Then, you can continuously check for .isPressed() and take another step in the appropriate direction. The library will make it very easy.

This part of your sketch makes the motor move one revolution:

    for (int x = 0; x < 2000; x++) { //2000 = 360 degrees at 400pulse/rev
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500); //sets speed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500); //sets speed
    }

Maybe try:

   // for (int x = 0; x < 2000; x++) { //2000 = 360 degrees at 400pulse/rev
     while (!p1buttonState || !p2buttonState) // !pxbuttonState is "pressed"
    { 
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500); //sets speed
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500); //sets speed
    }

No, don't try that. That would be an infinite loop since you never update your button state variables.

You need to read your buttons inside the loop.

Oops. I see my mistake. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.