2

I am having problems with a hard code example of accelerating a stepper motor. I can have the motor run at a constant speed but in order for it to start and not lose steps, I need an acceleration as it will move something relatively heavy. The code I have written does not seem to want to iterate the way it does without the additional code. The code is copied below in the void loop() section of the arduino code.

   `digitalWrite(dir, HIGH);
    for(int i=0; i <= distance; i++){
      Serial.println(i);
      if(i<1000){
        wait = 500 - (0.45*i);
      }
      else{
        wait = 50;
      }
      digitalWrite(pul, HIGH);
      delayMicroseconds(wait);
      digitalWrite(pul, LOW);
      delayMicroseconds(wait);
    }`

The program does not want to operate as written. The AccelStepper library is not a great option and this type of acceleration shouldn't be hard to add. I don't understand why this isn't working.

I have added continue; after the if and else statements as well and that does not fix my problem.

Also for decceleration my plan was to do the same in the opposite direction.

evan
  • 169
  • 3
  • 12
  • Here is an Arduino forum post on the matter, looks like they have solved it. Look into the code in the later posts https://forum.arduino.cc/index.php?topic=489323.0 – Haroun Hajem Aug 06 '19 at 09:24

1 Answers1

0

The SpeedyStepper Library (https://github.com/Stan-Reifel/SpeedyStepper)is a good option for you. I've been used for years, it has several options for controlling stepper motors, including acceleration and speed.