0

I am having difficulties programming what should be a simple sketch that gets my stepper motor to run at one step per second indefinitely at the press of a button and then stops at the press of another button. I am using the Bounce2.h and accelstepper.h libraries and have been unable to figure out how to do it. Any help would be greatly appreciated, and as for hardware I am using a NEMA 23 High Torque Stepper Motor connected through a M542T Motor Driver powered by a Switching Power Supply and running on an Arduino UNO.

Attatched find my code, and thank you so much for the help!

#include <Bounce2.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <AccelStepper.h>

AccelStepper stepper(1, 9, 8); // initiate stepper motor

//int buttonPresses = 0;
const byte ButtonPin = 10;
const byte LedPin = 13; // for debugging and visualization

Bounce button;

void setup() {
  Serial.begin(9600);
  button.attach(ButtonPin);
  pinMode(LedPin, OUTPUT);
  stepper.setSpeed(1);
}

void loop() {
  if (button.rose()) {
    stepper.runSpeed();
    digitalWrite(LedPin, HIGH);
    //    buttonPresses++; // count presses of button and print to monitor
    //    Serial.print("Number of Button ");
    //    Serial.print("Presses = ");
    //    Serial.println(buttonPresses);
  }
  else {
    button.update();
    digitalWrite(LedPin, LOW);
    if (button.fell()) {
      stepper.stop();
    }
  }
}

As of now, a button press runs the motor at 1 step/second, but a second press or any subsequent presses thereafter do nothing to stop the motor. Thanks so much!

!!EDIT!!

Upon adding a boolean buttonState and setting it to LOW in the setup and changing my void loop() code to look like:

void loop() {
  button.update();
  if (button.rose() == true && buttonState == LOW) {
    stepper.runSpeed();
    digitalWrite(LedPin, HIGH);
    buttonState = HIGH;
  }
  else {
    if (button.fell() == true && buttonState == HIGH) {
      stepper.stop();
      buttonState = LOW;
    }
  }
}

I know get a step every button press. Just thought it may help in answering.

EDIT 2

So I I tried pulling the runSpeed() out of the loop and adding if (buttonState = HIGH){stepper.runSpeed();} but that now just runs the stepperMotor constantly and at variable speed and will not turn it off?

EDIT 3 and Working Code Thank you Janw for the help. I realized my fault in calling the runSpeed function within the if statement. Further, I also needed to change button.fell() to button.rose() since button.fell() returns true when the button is released, so basically I was stopping the motor everytime I let go of the button. Here is the final, and working void loop() in case anyone runs into a similar problem in the future:

void loop() {
  button.update();
  if (button.rose() == true && buttonState == LOW) {
    digitalWrite(LedPin, HIGH);
    buttonState = HIGH;
  }
  else {
    if (button.rose() == true && buttonState == HIGH) {
      stepper.stop();
      buttonState = LOW;
    }
  }
  if (buttonState == HIGH) {
    stepper.setSpeed(1);
    stepper.runSpeed();
  }
}

1 Answer 1

0

You will probably get only 1 step each button press because your runSpeed() is within the if (button.rose() == true && buttonState == LOW)-statement.

The runSpeed() needs to be called every loop to check if the motor needs to step.

A simple way to change this is to set a boolean to true or false if you check your button and add something like this pseudocode:

if(motorRun == true) stepper.runSpeed();
3
  • check edits on initial question I have added some more!
    – tyler a
    Commented Jul 22, 2016 at 14:37
  • You need == instead of =
    – Janw
    Commented Jul 22, 2016 at 16:21
  • Okay I'll try that and see if that does the trick! Thanks!
    – tyler a
    Commented Jul 22, 2016 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.