I'm having issues controlling a stepper with push buttons; I'm sure it's something simple as I'm still learning to code.
I have a Nema 17 hooked up to a nano with a drv8825 driver. I'm using two 3D printer end stops as push buttons because that's what I had lying around. Everything seems to work with basic code so I think the hardware is fine. I'm trying to get it running so that the stepper is turning at a constant speed, until I push one of two buttons. If I push one button, I want the speed to increase very slightly; if I push the other, I want the speed to decrease very slightly. I'm currently trying to use the AccelStepper library because I figure that using the delay function will cause issues with reading the buttons (I need the stepper to turn very slowly, so long delays). Currently the stepper moves at a constant speed until I press either button, then runs at max speed and doesn't change. Here is my code so far:
#include <AccelStepper.h>
const int stepPin = 9; // Set the stepping pin to pin 9
const int dirPin = 8; // Set the direct pin to pin 8
const int btnOne = A2; // Set the first button pin to pin A2
const int btnTwo = A4; // Set the Second button pin to pin A4
int currentSpeed = 10; // Create a variable for speed
int btnOneState = 0; // Create a variable for reading button 1
int btnTwoState = 0; // Create a variable for reading button 2
#define motorInterfaceType 1
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
pinMode (btnOne, INPUT);
pinMode (btnTwo, INPUT);
myStepper.setMaxSpeed(1000);
myStepper.setSpeed(currentSpeed);
}
void loop() {
btnOneState = digitalRead (btnOne);
btnTwoState = digitalRead (btnTwo);
if (btnOneState == LOW){
currentSpeed += 1;
myStepper.setSpeed(currentSpeed);
myStepper.runSpeed();
}
else if (btnTwoState == LOW){
currentSpeed -= 1;
myStepper.setSpeed(currentSpeed);
myStepper.runSpeed();
}
else{
myStepper.runSpeed();
}
}
You need to sense when the switches become pressed rather than are pressed. Look at IDE -> file/examples/digital/state change detection. You should also probably be using INPUT_PULLUP for the switch pinModes.
Using SCD the speed will change by X increment only once per button press.
For visual feedback it wouldn't hurt to add a Serial.print statement or two to watch the speed value change.
Not positive, have no steppers here to try it with. But this may do what you are looking for...
#include <mechButton.h>
#include <idlers.h>
#include <AccelStepper.h>
#define BUTTON_PIN1 2 // Pin we'll hook a button to. The other side hooks to ground.
#define BUTTON_PIN2 3 // Pin we'll hook the other button to.
#define motorInterfaceType 1
const int stepPin = 9; // Set the stepping pin to pin 9
const int dirPin = 8; // Set the direct pin to pin 8
int currentSpeed = 10; // Create a variable for speed
mechButton button1(BUTTON_PIN1); // Set button one to pin 2.
mechButton button2(BUTTON_PIN2); // Set button two to pin 3.
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
// Your standard sketch setup()
void setup() {
button1.setCallback(myCallback1); // Set up #1's callback. (Don't use Analog for buttons.)
button2.setCallback(myCallback2); // Set up #2's callback.
myStepper.setMaxSpeed(1000);
myStepper.setSpeed(currentSpeed);
}
// This is the guy that's called when the button1 changes state.
void myCallback1(void) {
if (!button1.trueFalse()) {
currentSpeed += 1;
myStepper.setSpeed(currentSpeed);
}
}
// This is the guy that's called when the button2 changes state.
void myCallback2(void) {
if (!button2.trueFalse()) {
currentSpeed -= 1;
myStepper.setSpeed(currentSpeed);
}
}
// Your standard sketch loop()
void loop() {
idle(); // Let all the idlers have time to do their thing.
myStepper.runSpeed(); // Make the stepper go.
}
You'll need to grab LC_baseTools from the library manager to compile this.
Thanks, that worked! I had to add a line to include the Mechanical Switch library, and there was a typo where Button 1 was being called instead of Button 2, but I fixed that and it works perfectly