FlexyStepper - jogging button

Hello,

I am trying to drive a stepper motor by using FlexyStepper library from here: GitHub - Stan-Reifel/FlexyStepper: Stepper motor control library for Arduino supporting in motion changes

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.

Any advice is welcome, thank you

Seb

In this code

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();
}

...R

Thanks for your reply,

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();
        }
      }

Thank you

syHaimana:
Thanks for your reply,

i tested before, like you said in the last code, it is not doing the job

You need to post the complete version of your latest code.

You don't seem to have tried the suggestion I made at the bottom of Reply #1.

Trying code that is expected not to work is rather a waste of time :slight_smile:

...R

Thank you for your help

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

full code here:

#include <FlexyStepper.h>
int motorStepPin = 23;
int motorDirPin = 27;
bool previousButtonState;
bool buttonState;
FlexyStepper stepper;

void setup() {
  stepper.connectToPins(motorStepPin, motorDirPin);
  stepper.setSpeedInStepsPerSecond(5000);
  stepper.setAccelerationInStepsPerSecondPerSecond(1000);
  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);
  }
  if (buttonState == HIGH and previousButtonState == LOW) {
    stepper.setTargetPositionToStop();
  }
  stepper.processMovement();
}

Thank you

I'm not familiar with your library. Maybe you need these lines in setup()

    stepper.setCurrentPositionInSteps(0);
    stepper.setTargetPositionInSteps(0);

Please display your image(s) in your post so we can see it(them) without downloading it(them). See this Simple Image Posting Guide

...R

image inserted but it shows only reduced... not so useful...

i added these lines, even that one for stop, no effect... :frowning:

#include <FlexyStepper.h>
int motorStepPin = 23;
int motorDirPin = 27;
bool previousButtonState;
bool buttonState;
FlexyStepper stepper;

void setup() {
  stepper.connectToPins(motorStepPin, motorDirPin);
  stepper.setSpeedInStepsPerSecond(2000);
  stepper.setAccelerationInStepsPerSecondPerSecond(1000);
  pinMode(8, INPUT_PULLUP);
  stepper.setCurrentPositionInSteps(0);
  stepper.setTargetPositionInSteps(0);
  stepper.setTargetPositionToStop();
}

void loop() {
  previousButtonState = buttonState;
  buttonState = digitalRead(8);
  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();
}

syHaimana:
i added these lines, even that one for stop, no effect... :frowning:

Please always post a complete program. I'm not going to join bits together in case I introduce errors.

...R

previous post edited, full code added

thank you, Seb

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();
}

Thank you very much for your help,
Seb

syHaimana:
I really think is a FlexyStepper library issue.
I changed it with SpeedyStepper (same developer) and is working as expected.

Interesting.

...R