I have the following bit of code where I want to control the rate of acceleration. I'm using the Accel stepper library. The code runs but I see no change to the ramp up rate. What have I done wrong?
#include <AccelStepper.h>
AccelStepper stepper(9,8);
int Distance = 0; // Record the number of steps we've taken
void setup() {
stepper.setAcceleration(10);
stepper.setMaxSpeed(150);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 10000)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(2000);
}
}
If I set the distance over a value of 32700 the stepper never stops. Can someone explain this and offer a solution?
int Distance = 0; // Record the number of steps we've taken
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, HIGH);// change to LOW to change direction
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(82);
digitalWrite(9, LOW);
delayMicroseconds(82);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 32700)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(2000);
}
}
Why have you two Threads about the same code. Other Thread
If you want to use the Accelstepper library start with one of its examples. If you don't understand the example post the code along with a description of what you don't understand.
The code in your Original Post in this Thread is completely wrong for the AccelStepper library.
Im using a gearhead on the stepper motor so yes, I do need that many steps. Someone suggested making "distance unsigned long". Can you write the correct coding for that? I assume I would replace "int distance = 0" with this?
Also, I did try and run code that works with the arduino stepper library with accel library because I wanted the ramping feature. I take it this is not kosher. I can start with the accel "random" program, but I have a question:
stepper.moveTo (rand() % 200);
If I omit the word(rand) and replace with a value, will the stepper move to that value? If so I can try to write what I need from that base.
Widpyro:
If I omit the word(rand) and replace with a value, will the stepper move to that value? If so I can try to write what I need from that base.
If you read the manual stepper.moveTo() sets the target position but it does not make anything move. You need to call stepper.run() repeatedly (as often as possible and certainly faster than the minimum step interval) to cause the movement to happen.
I don't think AccelStepper is capable of step intervals as low as 164 µsecs.
Can we assume that the earlier question about 32700 steps is no longer relevant?