Go Down

Topic: "Bounce" in one direct. in AccelStepper.h (Read 167 times) previous topic - next topic

fexr3i

Hello,
I need code that make stepper moving 1000 distance constantly in the same direction but with 2 secs of break between these moves,

the code:
Code: [Select]
#include <AccelStepper.h>
#define HALFSTEP 8

#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(75.0);
  stepper1.setSpeed(100);
  stepper1.moveTo(1000);
}

void loop()
{   
stepper1.run();
delay(2000);
}


doesn't work because motor makes 2-sec. break after every single step(I can see it by LEDs on motor driver), not after moving all distance,

could you explain what is wrong and how to fix it?

MarkT

You must never use delay() with AccelStepper library while a motor is moving.


Perhaps you should be using runToPosition()?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

fexr3i

Can runToPosition() be used in loop? I still see errors when I try.

Also I tried to modify this code:

Code: [Select]
// Bounce.pde

#include <AccelStepper.h>

AccelStepper stepper;
void setup()

  stepper.setMaxSpeed(100);
  stepper.setAcceleration(20);
  stepper.moveTo(500);
}
void loop()
{
   if (stepper.distanceToGo() == 0)
    delay(2000);
     stepper.moveTo(-stepper.currentPosition());
      delay(2000);
   stepper.run();
}


by removing - <minus> from

Code: [Select]
stepper.moveTo(-stepper.currentPosition());

but then of course nothing happened after these 2 secs, have you any more ideas?

MorganS

It's always going to go the same direction? Like for thousands of revolutions after many days?

Then each time you stop, reset the "current" position to zero. Then set the target to the value you want and call runToPosition().
"The problem is in the code you didn't post."

fexr3i

#4
Oct 17, 2018, 10:36 pm Last Edit: Oct 17, 2018, 10:42 pm by fexr3i
Ok, I've got it, maybe it's done in stupid way but works like I wanted it to(moving 1000 clockwise, wait 2 secs, again 1000 clockwise and again 2 secs - in loop with acceleration and deacceleration).


Code: [Select]
#include <AccelStepper.h>
#define HALFSTEP 8

#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
  stepper1.setMaxSpeed(500.0);
  stepper1.setAcceleration(75.0);
  stepper1.setSpeed(100);
  stepper1.moveTo(1000);

}

void loop() {

  if (stepper1.distanceToGo() == 0) {
    delay(2000);
    stepper1.move(1000);
    delay(2000);
  }
  stepper1.run();
}


MorganS Yes, all time the same direction, I tried runToPosition(), but there still were errors.

MorganS

If you don't tell us what the errors were, we can't help you solve the problems.
"The problem is in the code you didn't post."

fexr3i

The error in code with runToPosition() was

"error: no matching function for call to 'AccelStepper::runToPosition(int)'",

but code with
Code: [Select]
...
if (stepper1.distanceToGo() == 0) {
    delay(2000);
    stepper1.move(1000);
    delay(2000);
...

I've posted above works fine and does what I wanted.

Go Up