Accelerates the engine to start and decelerates to stop

Hey! I've got a question. Is it possible to convert the code into start and stop of the engine so that it accelerates when starting and slows down when stopping?
Graph:

Code:

// DEFINE PINOW //

#define dirPinA 2
#define stepPinA 3

#define dirPinB 4
#define stepPinB 5

#define dirPinC 11
#define stepPinC 12



void setup()
{

   // OUTPUT PINY //

   pinMode(stepPinA, OUTPUT);
   pinMode(dirPinA, OUTPUT);

   pinMode(stepPinB, OUTPUT);
   pinMode(dirPinB, OUTPUT);

   pinMode(stepPinC, OUTPUT);
   pinMode(dirPinC, OUTPUT);

   // KIERUNEK //
}

void loop()
{
// OPENING ROBOTIC ARM //
  
   digitalWrite(dirPinC, HIGH);

   for (int n = 0; n < 400; n++)
   { 
      stepC();  
   }

 
///////////////
   digitalWrite(dirPinA, HIGH);

   for (int n = 0; n < 400; n++)
   { 
      stepA();  
   }


///////////////
   digitalWrite(dirPinB, HIGH);

   for (int n = 0; n < 400; n++)
   { 
      stepB();  
   }

   delay(1000);
///////////////

// CLOSING ROBOTIC ARM //

  digitalWrite(dirPinB, LOW);

   for (int n = 0; n < 400; n++)
   { 
      stepB();  
   }

///////////////
   digitalWrite(dirPinA, LOW);

   for (int n = 0; n < 400; n++)
   { 
      stepA();  
   }

///////////////
   digitalWrite(dirPinC, LOW);

   for (int n = 0; n < 400; n++)
   { 
      stepC();  
   }

   delay(3000);

}

void stepA()
{
   digitalWrite(stepPinA, HIGH);
   delay(2);
   digitalWrite(stepPinA, LOW);
   delay(2);
}

void stepB()
{
   digitalWrite(stepPinB, HIGH);
   delay(2);
   digitalWrite(stepPinB, LOW);
   delay(2);
}

void stepC()
{
   digitalWrite(stepPinC, HIGH);
   delay(2);
   digitalWrite(stepPinC, LOW);
   delay(2);
}


Isn't what engines do anyway when they start and stop?

the engine starts at 400 rpm. I want the speed to increase 0-400

Yes it is.

If you are using stepper motors, the AccelStepper library uses acceleration, though the slope may not be linear.

Also the MobaTools library has a stepper class with acceleration.

And this thread will help you to write your own acceleration code. See reply #8.

1 Like

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