Setting steps with external button

Status
Not open for further replies.

bradmcco

Member
Hi,
I am using a Teensy 3.6 to control 2x Nema 34 Stepper motors through two drivers. I have the STEP and DIR pins connected to the Teensy along with a UP and Down Button.

I would like the motors to move up while holding the UP button and the motors to move down while holding the down button.
I am having issues writing the correct code for this, as it results in the motor going very slow & the motor is jumping/skipping.

Thanks
 
Showing the code you have might lead to a more helpful resolution
Note: _ I removed a duplicate of this post to avoid confusion
 
Code:
#include <StepControl.h>

Stepper motor_1(2, 3);   //STEP pin =  2, DIR pin = 3
Stepper motor_2(9,10);   //STEP pin =  9, DIR pin = 10

StepControl<> controller;
  int up=5;

void setup() {
    int up=5;
  pinMode(up,INPUT_PULLUP);

  // put your setup code here, to run once:
 motor_1
    .setMaxSpeed(50000)       // steps/s
    .setAcceleration(200000); // steps/s^2 
  
  motor_2
    .setMaxSpeed(50000)       // steps/s
    .setAcceleration(200000); // steps/s^2 
}

void loop() {
  // put your main code here, to run repeatedly:
 if (digitalRead(up)==LOW)
  {
    motor_1.setTargetRel(100);
    motor_2.setTargetRel(100);
  }
  controller.move(motor_1,motor_2);
}
 
Last edited by a moderator:
Thanks! I will have a read!

Will this allow me to have continuous rotation of my motor without any acceleration/deceleration between loops
 
I recommend to start with a basic example to check if your hardware is working correctly. (It shouldn't jump, and it should move quite quick with the settings you posted)
https://github.com/luni64/TeensyStep/blob/master/examples/HelloStepper/HelloStepper.ino

Using that sketch you can also try to find out max speed and max acceleration and get a feeling for the motors. Start with the values from the example and increase them until the motor will loose steps, i.e. until it stops rotating during the acceleration phase. Also adjust the motor current to reasonable values.
 
Hi Luni,

I have no dramas getting the stepper motors to move.
The problem is using an up and down button to move them.
When running .setTargetAbs(5000000); the motors run smooth and fast.

When i add the if statement for the up or down button, the motors turn at a very slow speed and are jolting/skipping.

I require the motors to move when the button is being held and to stop when let go.
 
Here an example how to control your two motors with up / down buttons. It uses the Bounce2 library to debounce the switches. The function getSwitchState() returns a combined state of the switches (none, up, down, both). In loop the code simply checks if the state of the switches changed. If so, it sets the motor direction accordingly. Code assumes active LOW switches.

Code:
#include "Arduino.h"
#include "Bounce2.h"
#include "TeensyStep.h"

// motors
constexpr unsigned stpPin1 = 0, dirPin1 = 1;
constexpr unsigned stpPin2 = 2, dirPin2 = 3;

RotateControl controller;
Stepper m1(stpPin1, dirPin1), m2(stpPin2, dirPin2);

// switches
constexpr unsigned upSwitchPin = 4;
constexpr unsigned downSwitchPin = 5;
Bounce upSwitch, downSwitch;

enum SwitchState{none, up, down, both};

SwitchState oldState = SwitchState::none;

void setup()
{
  // setup switches
  upSwitch.attach(upSwitchPin, INPUT_PULLUP);  
  downSwitch.attach(downSwitchPin, INPUT_PULLUP);  

  // setup motors
  m1
      .setMaxSpeed(20000)
      .setAcceleration(10000);
  m2
      .setMaxSpeed(20000)
      .setAcceleration(10000);

  controller.rotateAsync(m1, m2);
  controller.overrideSpeed(0);      // start with stopped steppers
}

void loop()
{
  SwitchState newState = getSwitchState();

  if (newState != oldState)
  {
    switch (newState)
    {
    case up:
      controller.overrideSpeed(1.0f);
      break;

    case down:
      controller.overrideSpeed(-1.0f);
      break;

    case none:
      controller.overrideSpeed(0.0f);
      break;

    case both:
      break;
    }

    oldState = newState;
  } 
}

// helpers -----------------------------------------------------

SwitchState getSwitchState()
{
  upSwitch.update();
  downSwitch.update();

  // Active LOW switches 

  if (upSwitch.read() == HIGH && downSwitch.read() == LOW)
    return SwitchState::down;

  if (upSwitch.read() == LOW && downSwitch.read() == HIGH)
    return SwitchState::up;

  if (upSwitch.read() == LOW && downSwitch.read() == LOW)
    return SwitchState::both;

  return SwitchState::none;
}

(Note: you need to use the TeensyStep from the develop branch of the library. I try to release this branch as soon as possible but there are a few things open which might require a change of the interface)

PS: Thanks for offering, but no need for compensation, this is all fun...
 
Last edited:
what does f do in the speed setting?
The f is a so called literal suffix, i.e. it is the c/c++ way of telling the compiler that the constant is a float. Without the f a literal like 1.23 would default to double. See e.g. here for details: https://www.embedded.com/electronics-blogs/programming-pointers/4403282/Numeric-Literals

Technically it is not necessary here since
  1. In a function call the compiler would convert the double literal at compile time anyway
  2. Teensyduino implements a compiler switch which treats all floating point literals as float instead of the usual double.
So, in this case it is just a matter of taste to use the f suffix and you can safely remove it if it hurts your eyes :)


Do your motors run as required with the code?
 
the use of f suffix, or any other is not just a matter of taste, but allows your code to work on other platforms as well that for example don’t have the compiler switch etc
 
Status
Not open for further replies.
Back
Top