I need a "FEED" button to make the motor start rotating (with ramping up, too) and keep moving while the button is pressed and to stop the motor when the button is released (with deceleration, too)
I have the following code which start the motor but it doesn't stop it. It seems the stepper.setTargetPositionToStop() function is not working.
Some comments into the code. Tested with Mega 2560.
#include <FlexyStepper.h>
#include <Button.h>
int motorStepPin = 23;
int motorDirPin = 27;
FlexyStepper stepper;
Button bFEED = Button(8, PULLUP);
void setup() {
stepper.connectToPins(motorStepPin, motorDirPin);
stepper.setSpeedInStepsPerSecond(1000);
stepper.setAccelerationInStepsPerSecondPerSecond(500);
}
void loop() {
if (bFEED.isPressed()) {
stepper.setCurrentPositionInSteps(0);
stepper.setTargetPositionInSteps(10000);
while (!stepper.motionComplete()) {
stepper.processMovement();
if (!bFEED.isPressed()) {
// any other commands here are working but not this one
// if this command is commented, the stepper will move 10000 (as requested) and stop
// if this command is not commented the stepper keep move forever
stepper.setTargetPositionToStop();
}
}
}
}
The original code is much larger and is running ok, excepting this part. I made this small code just for testing the FEED button.
void loop() {
if (bFEED.isPressed()) {
stepper.setCurrentPositionInSteps(0);
stepper.setTargetPositionInSteps(10000);
while (!stepper.motionComplete()) {
stepper.processMovement();
if (!bFEED.isPressed()) {
// any other commands here are working but not this one
// if this command is commented, the stepper will move 10000 (as requested) and stop
// if this command is not commented the stepper keep move forever
stepper.setTargetPositionToStop();
}
}
}
}
there is nothing to update bFEED in the WHILE loop.
Also, because all the code is inside the if (bFEED.isPressed()) it is impossible for bFEED to be not pressed
I was going to suggest this
void loop() {
if (bFEED.isPressed()) {
stepper.setCurrentPositionInSteps(0);
stepper.setTargetPositionInSteps(10000);
}
if (!bFEED.isPressed()) {
stepper.setTargetPositionToStop();
}
stepper.processMovement();
}
but it won't work either because you should not be setting the target all the time the button is pressed. You only want to do that when the button changes from not-pressed to pressed. I am not familiar with the button library so I can't suggest how that should be done using it.
Without using the library I would do it like this
void loop() {
previousButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW and previousButtonState == HIGH) { // assumes LOW when pressed
stepper.setCurrentPositionInSteps(0);
stepper.setTargetPositionInSteps(10000);
}
if (buttonState == HIGH and previousButtonState == LOW) {
stepper.setTargetPositionToStop();
}
stepper.processMovement();
}
i tested before, like you said in the last code, it is not doing the job
regarding your first observation... i added a Serial.print line, like this, to check that "if" and is working...
I mean the "Button UP" text is shown in monitor...
But the setTargetPositionToStop have no effect.
while (!stepper.motionComplete()) {
stepper.processMovement();
if (!bFEED.isPressed()) {
Serial.println("Button UP")l
// any other commands here are working but not this one
// if this command is commented, the stepper will move 10000 (as requested) and stop
// if this command is not commented the stepper keep move forever
stepper.setTargetPositionToStop();
}
}
I reinstalled arduino and flexystepper library, no button library installed/used
Now is better but still have some issues
when arduino is started, i have continuous pulses on STEP pin and the direction pin is inverted
if bFEED button not pressed, will have this pulses forever
after i press the bFEED button, after some time, the pulses are stopping.
as long as i delay pressing the button, as long i have to wait after that for pulses to stop
i made a screenshot from the logic analyzer with some comments inside, see attachment
I really think is a FlexyStepper library issue.
I changed it with SpeedyStepper (same developer) and is working as expected.
I had to change the acceleration to a bigger value not to have these delays after releasing button, much bigger than I used before. I'll try with physical stepper connected but for now it seems to have the proper signals.
In the following code i commented out the FlexyStepper related lines and I add after them the SpeedyStepper related lines.
//#include <FlexyStepper.h> // for easystepper
#include <SpeedyStepper.h> // for speedystepper
int motorStepPin = 23;
int motorDirPin = 27;
bool previousButtonState;
bool buttonState;
//FlexyStepper stepper; // for easystepper
SpeedyStepper stepper; // for speedystepper
void setup() {
stepper.connectToPins(motorStepPin, motorDirPin);
stepper.setSpeedInStepsPerSecond(2000);
stepper.setAccelerationInStepsPerSecondPerSecond(50000);
pinMode(8, INPUT_PULLUP);
}
void loop() {
previousButtonState = buttonState;
buttonState = digitalRead(8);
if (buttonState == LOW and previousButtonState == HIGH) { // assumes LOW when pressed
stepper.setCurrentPositionInSteps(0);
// stepper.setTargetPositionInSteps(10000); // for easystepper
stepper.setupMoveInSteps(50000); // for speedystepper
}
if (buttonState == HIGH and previousButtonState == LOW) {
// stepper.setTargetPositionToStop(); // for easystepper
stepper.setupStop(); // for speedystepper
}
stepper.processMovement();
}