3

At the moment I am working on a code that should send a plateau to a certain position and back. The plateau is being moved by the stepper, in my case a Nema 17 with a Polulu DRV8825 driver. Everything is hooked up correctly. My goal is to press '4' on the keyboard and that then the stepper moves to a certain position. However, I am not succeeding in this. I am using AccelStepper and at the moment I have the following code:

#include <AccelStepper.h>
AccelStepper stepper(1, 5, 4);
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, 0 or -1 
int buttonState = 0;
void setup()
{
 Serial.begin(9600);
 stepper.setMaxSpeed(1000);
 stepper.setSpeed(1000);
}
void loop()
{
 char c;
 if (Serial.available()) {
 c = Serial.read();
 if (c == 'f') { // forward
 sign = 1;
 }
 if (c == 'r') { // reverse
 sign = -1;
 }
 if (c == 's') { // stop
 sign = 0;
 }
 if (c == '1') { // super slow
 spd = 100;
 }
 if (c == '2') { // medium
 spd = 900;
 }
 if (c == '3') { // fast
 spd = 1000;
 }
 if (c == '4') {
 //not working, does anyone know how to do this?
 stepper.moveTo(500);
 }
 stepper.setSpeed(sign * spd);
 }
 stepper.runSpeed();
}

Does anyone have tips or know how to achieve this?

Thanks is advance!

zx485
2564 silver badges15 bronze badges
asked Dec 11, 2015 at 8:13
1
  • You need to call stepper.run() instead of runSpeed, for moveTo to work. Commented Dec 11, 2015 at 15:28

1 Answer 1

1

Gerben's comment:

You need to call stepper.run() instead of runSpeed, for moveTo to work.

provides the answer. The updated code is:

#include <AccelStepper.h>
AccelStepper stepper(1, 5, 4);
int spd = 1000; // The current speed in steps/second
int sign = 1; // Either 1, 0 or -1
int buttonState = 0;
void setup()
{
 Serial.begin(9600);
 stepper.setMaxSpeed(1000);
 stepper.setSpeed(1000);
}
void loop()
{
 char c;
 if (Serial.available()) {
 c = Serial.read();
 if (c == 'f') { // forward
 sign = 1;
 }
 if (c == 'r') { // reverse
 sign = -1;
 }
 if (c == 's') { // stop
 sign = 0;
 }
 if (c == '1') { // super slow
 spd = 100;
 }
 if (c == '2') { // medium
 spd = 900;
 }
 if (c == '3') { // fast
 spd = 1000;
 }
 if (c == '4') {
 //not working, does anyone know how to do this?
 stepper.moveTo(500);
 }
 stepper.setSpeed(sign * spd);
 }
 stepper.run(); //You need to call stepper.run() instead of runSpeed, for moveTo to work. – Gerben
}
answered Feb 28, 2016 at 17:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.