Steps viewer with Serial Monitor and runToPosition()

2,075 views
Skip to first unread message

Edgar Martinez

unread,
Mar 14, 2012, 6:30:24 AM3/14/12
to accelstepper
Hi Everbody,
I was testing the function runToPosition(); to make linear
acceleration/deceleration ramps und it works very very good,
My isuue is;

With the following code i get just the last position , and I want to
be able to see each step with the arduino´s serial Monitor.
The documetation says "Dont use runToPosition() in event loops, since
it blocks.

So the other way could be use a encoder to read each step?
is it posible?

thanks..
Eddy







#include <AccelStepper.h>
AccelStepper stepper(1,3,2);
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(100);
}
void loop ()
{

stepper.moveTo(1000);

stepper.runToPosition();
Serial.println(stepper.currentPosition());
}

Mike McCauley

unread,
Mar 14, 2012, 6:58:32 AM3/14/12
to accels...@googlegroups.com
Hi,

I think you would have to do something like (not tested)

void loop ()
{
stepper.moveTo(1000);

long lastPosition = 0;
while (stepper.distanceToGo())
{
run();
long thisPosition = stepper.currentPosition();
if (thisPosition != lastPosition)
{
Serial.println(thisPosition);
lastPosition = thisPosition;
}
}
while(1); // done
}

But its not a very good idea, because the cost of the print might interfere
with the correct operation of the stepper.

--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

Edgar Martinez

unread,
Mar 14, 2012, 12:33:29 PM3/14/12
to accelstepper
Hi Mike,
thanks thanks

it works very cool I
I see each the value in the Serial Monitor. baudrate==115200
of course in really high speeds not of them but my aplication is
really good;

Thank you so much
cheers.
Eddy

MasseyMan

unread,
Mar 16, 2012, 1:17:18 AM3/16/12
to accelstepper
Works well as long as you set "stepper.setMaxSpeed", without it it
moves about 1 step per second. With it, it runs fast and prints all
step locations.

Example;

#include <AccelStepper.h>

AccelStepper stepper; // Defaults to 4 pins on 2, 3, 4, 5

void setup()
{
stepper.setMaxSpeed(200);
stepper.setSpeed(120);
Serial.begin(115200);
}

void loop ()
{
stepper.moveTo(100);
long lastPosition = 0;
while (stepper.distanceToGo())
{
stepper.runSpeed();
long thisPosition = stepper.currentPosition(); //grab current
position
if (thisPosition != lastPosition)
{
Serial.println(thisPosition); //print position
lastPosition = thisPosition;
}
}
while(1); // done
}


Does anyone know why this is? Looking through the documentation I
didn't think "setSpeed" relied on "setMaxSpeed".

Mike McCauley

unread,
Mar 16, 2012, 7:19:21 PM3/16/12
to accels...@googlegroups.com
Hi,

If AccelStepper ever tries to calculate speeds, it limits the calculated speed
according to maxSpeed.

moveTo() calculate a new speed, and therefore maxSpeed is used at that time.
Suggest you set the speed after every call to moveTo

Cheers.

S Ma

unread,
Apr 15, 2018, 1:21:10 PM4/15/18
to accelstepper
Hello Mike,
sorry for replying to this old post.
I am trying to use AccelStepper library and not sure what am I doing wrong, but with my code below, I cannot control the speed of motors.
they seems dividing the speed.
the reason I am saying they are dividing is because I have another arduino sketch and I am able to control the speed the just fine.
here is brief explanations:
I need to run two nema 34 motors simultaneously in opposite directions. when one of them is hitting photo interrupter - both motors should change directions.
sounds easy, but definitely something is wrong in my sketch.
Could you, please help?
The below is sketch with wiring.
Thank you in advance!

/*
*photo interrupter
 * PI(grey) -> 5V
 * PI(white) -> pin12
 * PI(black) -> GND
 * motor1
 * DIR-     -> pin4
 * PULL-    -> GND
 * PULL+    -> pin5
 *
 * motor2
 * DIR-    -> pin2
 * PULL-   -> GND
 * PULL+   -> pin3
 */
 #include <AccelStepper.h>
int ph=12;
int pushCounter=0;
int state=0;
int lastState=0;
float spd=1900.0;
float maxspd=2000.0;
AccelStepper m1(1,3,2);
AccelStepper m2(1,5,4);
void setup(){
  delay(5);
  pinMode(ph,INPUT);
  Serial.begin(9600);
  m1.setMaxSpeed(maxspd);
  m2.setMaxSpeed(maxspd);
}
void loop(){
  do{
    if(pushCounter > 1){
    pushCounter=0;
  }
  getCounter();
    m1.runSpeed();
    m2.runSpeed();
  }while(Serial);
}
void getCounter(){
  state=digitalRead(ph);
  if(state !=lastState){
    if(state !=0){
      pushCounter +=1;
    }else{
      ;
    }
  }
  lastState=state;
  if(pushCounter % 2 == 0){
    Serial.println("forward");
    m1.setSpeed(spd);
    m2.setSpeed(-spd);
  }else{
    Serial.println("backward");
    m1.setSpeed(-spd);
    m2.setSpeed(spd);
  }
}
#################################

Mike McCauley

unread,
Apr 16, 2018, 4:28:26 AM4/16/18
to accels...@googlegroups.com
Hello,

If you are reporting a bug, then you might want to look at:

http://en.wikipedia.org/wiki/Wikipedia:Reference_desk/
How_to_ask_a_software_question
http://catb.org/~esr/faqs/smart-questions.html
http://www.chiark.greenend.org.uk/~shgtatham/bugs.html

If you are asking me to help write your software then I dont think I can help
you .

Cheers.
> > Radiator: the most portable, flexible and configurable RADIUS server
> > anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
> > Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
> > TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
> > DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare
> > etc.


--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com 5R3MRFM2+X6
Phone +61 7 5598-7474

gregor

unread,
Apr 16, 2018, 6:37:31 AM4/16/18
to accelstepper
Hi,

you don't need the while loop in function loop(), you may want to remove it
don't call runSpeed() conditionally, only use setSpeed() to control the steppers
Serial output is slow, don't use it if you need high (or accurate) speeds

S Ma

unread,
Apr 16, 2018, 11:59:02 AM4/16/18
to accelstepper
Hi Mike,
no, I am not reporting any bugs.
and yes, I was asking for your help as you wrote this AccelStepper library.
and, of course - if you don't want to help - that's fine.
Have a nice day.
Regards,
Slava

S Ma

unread,
Apr 16, 2018, 12:04:43 PM4/16/18
to accelstepper
Hi Gregor,
Thank you for replying.
I will try your suggestion - thank you.
Thing is that it all work just fine without photo interrupter. As soon photointerrupter is included, motors start moving slowly and changing speed in the code has no effect on the output.
By saying "don't use serial" - do you mean that I used it as a condition of the loop?
Do you suggest that it is a source of the problem?

Thank you,
Slava


On Monday, April 16, 2018 at 3:37:31 AM UTC-7, gregor wrote:
Hi,

you don't need the while loop in function loop(), you may want to remove it
don't call runSpeed() conditionally, only use setSpeed() to control the steppers
Serial output is slow, don't use it if you need high (or accurate) speeds.

S Ma

unread,
Apr 16, 2018, 12:32:59 PM4/16/18
to accelstepper
Hi Gregor,

Thank you so, so much!
I changed default "9600" to 115200 - and code works as I wanted to.
Thank you very much for pointing me to this!

Regards,

Slava
On Monday, April 16, 2018 at 3:37:31 AM UTC-7, gregor wrote:

Daniel Tremblay

unread,
Mar 19, 2019, 9:28:14 AM3/19/19
to accelstepper
Hi

I already mooved my wifiudp to a separate task on ESP32, it was jamming my loop,now it is smooth!

i added in my task:
        long currentPosition = stepper.currentPosition();
        Serial.println(currentPosition);
it dont look to slow anything!
Multi task over 2 cores is amazing!

Daniel
Reply all
Reply to author
Forward
0 new messages