Stepper motor acceleration code

Hello everyone,
I'm looking to create an acceleration and deceleration code for a stepper motor.
Here you can find the code :

#define EN2 8
#define DIR2 9
#define ST2 10


void setup(){
pinMode(EN2, OUTPUT);
pinMode(DIR2, OUTPUT);
pinMode(ST2, OUTPUT);
Serial.begin(115200);

}

void loop(){
int D2 = 200;
while (true){
digitalWrite(EN2, HIGH);
digitalWrite(DIR2, HIGH);
float delay2 = ((1/D2)*1000000);
D2 += 100;
if (D2 > 3200){D2 = 3200;}
for (int i = 0; i <= 200; i++) {
digitalWrite(ST2, HIGH);
delayMicroseconds(delay2);
//delay(1);
digitalWrite(ST2, LOW);
//delay(1);
delayMicroseconds(delay2);}
  } 
}

After uploading this code, the stepper doesn't rotate despite hearing the sound of a continuous frequency.
And I want to specify that replacing "delay2" by 5000 the stepper rotating fine but without acceleration.
Any suggestion can help me.
Thank you in advance.

Integer math makes zeros here.

Multiply before the division in integer math.
And since you are using a integer result, you could do the math in integers:

//float delay2 = 1000000.0/D2;
unsigned long delay2 = 1000000UL/D2;
//Serial.println(delay2); // for debugging

Thank you very much DaveX
That worked as expected.

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