//https://forum.arduino.cc/t/stepper-motor-s-curve/465667
long ta = 3e5;     // acceleration time (microsec)
long td = 3e5;
;     // deceleration time (microsec)
long Vm = 10;    // steady state velocity (pulse/sec)
long Pt = 1600;    // total number of pulses for move (1600 steps per rev)
// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0 so you don't calculate too long of a delay
long t12 = 1e6 - ta - td;           // time during constant velocity (microsec)
int count = 0;
int loopcount = 0;
int dircount = 0;
const float loopdelay = 5000;
#define dirPin 13  //red=dirPin, white=stepPin, black=digitalGND
#define stepPin 3
void setup() {
  
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 
}
void loop() {
  loopcount++;
if (loopcount == Pt){
  dircount++;
  
  if ((dircount % 2) == 0){
    digitalWrite(dirPin,HIGH);
  }
  else {
    digitalWrite(dirPin,LOW);
  }
}
else{
  
 // Decide which part of the velocity curve your at
 if (t<ta){                                       // Acceleration
   dly = (ta)/(2*(Vm/10)*t);
 } 
 else if (t>=ta && t<(ta+t12)){                    // Constant velocity
   dly = 1/(2*(Vm/1e6));
 }
 else if (t>=(ta+t12) && t<(ta+t12+td)){           // Slow down
   dly = 1/(2*((Vm/10)-(Vm/(1e6*td))*(t-ta-t12)));
 }
 
 t = t+2*dly; // update the current time
 
 // Move stepper one pulse using delay just calculated
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(dly);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(dly);
 count ++;
 
 // The move is finished
 if (t>=(ta+t12+td)){
  
   loopcount=0;
   count=0;
   t=td/9;
 
   delay (loopdelay);
 }
}
}