Controlling stepper ramp up

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);
  }
}

You don't understand the AccelStepper library and are bit-banging the pins it is supposed
to be controlling.

You need study the the moveTo(), currentPosition() and run() methods. Don't touch pins 8 and 9, just pass
them to the library.

so I should use pins 0,1 for the motor commands?

You don't use any pins except in this line:

AccelStepper stepper(9,8);

That's it, you just call methods on stepper.

MarkT:
You don't use any pins except in this line:

AccelStepper stepper(9,8);

That's it, you just call methods on stepper.

Did you look at the examples that come with AccelStepper? If not then do so.

Yes I have and I just went over it. Im not clear why the acceleration is not working.

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);
  }
}

Make distance
unsigned long

int Distance = 0;  // Record the number of steps we've taken

An int in the Arduino cannot exceed 32767.

is my math right ?
it will take about 45 minutes ?

12 steps per second , 2,735 seconds

op's, delay is in microseconds,

the sketch shows 32,700 as the end of travel,
int can count to 32,700 and have 67 counts to spare.

I think the OP picked a number short of the maximum .

Widpyro:
If I set the distance over a value of 32700 the stepper never stops. Can someone explain this and offer a solution?

What is the maximum number that works correctly ?
And have you carefully counted the actual steps taken to be sure that none is missed ?

164 µsecs is a very short interval between steps. 0.164 millisecs between steps means 6097 steps per second. Are you using microstepping ?

The logic of the timing would be easier to follow if you put all the "delay" in one place - for example

 digitalWrite(9, HIGH);
  delayMicroseconds(10);         
  digitalWrite(9, LOW);
  delayMicroseconds(154);

You should also be aware that digitalWrite() is a slow function and you probably don't need any delay between the two digitalWrite()s

...R

dave-in-nj:
the sketch shows 32,700 as the end of travel,
int can count to 32,700 and have 67 counts to spare.

I think the OP picked a number short of the maximum .

He didn't say it worked at 32701 counts. Probably he bumped up the counts by 100 or so.

Works at 32700.
Doesn't work at 32800.

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.

...R

Threads merged.

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?

...R

Robin says:

"Can we assume that the earlier question about 32700 steps is no longer relevant?"

Ive decided to go with a larger nema 34 stepper and forgoe the gear reduction, hence less steps needed.