How can i create a function to accelerate the stepper motor at given acceleration?

Here is my code that basically, it decreases the time between steps ON/OFF for every rotation of the stepper. Using the for loop and decreasing "Z" witch is time ON/OFF.

#define dirPin 8
#define stepPin 9

#define dirPin1 4
#define stepPin1 5

const int NofSteps=3200;

const int StepXrev=400;
float RPM=300;
float z=(((60/RPM)/(StepXrev))/(2))*1000000;
int f=0;
int i=25;
void setup() {

pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, LOW); 
pinMode(stepPin, OUTPUT);

pinMode(dirPin1, OUTPUT);
digitalWrite(dirPin1, LOW); 
pinMode(stepPin1, OUTPUT);

}

void loop() {
  
for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z); 
  }  f=f+i;

for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f); 
  }  f=f+i;


  for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f); 
  }  f=f+i;

    for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f); 
  }  f=f+i;

    for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f); 
  }
    f=f+i;

      for(int n=0;n<NofSteps;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f); 
  }

  f=f+i;
        for(int n=0;n<100000;n++){
  digitalWrite(stepPin, HIGH); 
  digitalWrite(stepPin1, HIGH);   
  delayMicroseconds(z-f);                       
  digitalWrite(stepPin, LOW);    
  digitalWrite(stepPin1, LOW);    
  delayMicroseconds(z-f);
 
  }
    f=0;



}//.8sec/rev

Hi,
Try a library called AccelStepper, a very well documented library.
https://www.airspayce.com/mikem/arduino/AccelStepper/

Tom... :smiley: :+1: :coffee: :australia:

I did try it, but you can't control two steppers at same time. That's why I'm creating a separate code

Hi,

Did you Google?

accelstepper with two steppers

Tom... :+1: :smiley: :coffee: :australia:

Did you look at the AccelStepper examples? Namely the multipleSteppers example? It shows how to run 3 steppers at the same time with acceleration.

There is also the MobaTools stepper library that uses acceleration.

Robin2 has simple stepper code with acceleration that does not use blocking delays in this thread. See post #8.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.