How to make stepper motor accelerate and decelerate easy way?

Hi! I am not a programmer and having a hard time trying to make my stepper motor move the way I need.

I have two tact switches connected to Pins 5 and 6 called button1 and button2. When clicked they are supposed to make motor move to some places and go back to starting position. It works fine but I need to change this code to make my motor accelerate when starting and decelerate before reaching the end point.

I would appreciate any help.

This is my code:

// define the pins
#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
#define button1 5
#define button2 6

// define how many cycles to a full rotation
#define CYCLES_PER_ROTATION 512

void setup() 
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT); 
  pinMode(IN4, OUTPUT); 
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);

}

void loop() 
{
  while (digitalRead(button1) == LOW) {
  // make the stepper do 0.5 rotations CW
  turns(0.5);
  delay(500);
  // make the stepper do 1.5 rotations CCW
  turns(-1.5);
  delay(500);
  turns(1.5);
  delay(500);
  turns(-0.5);
  delay(500);
  }

  while (digitalRead(button2) == LOW) {
    turns(0.5);
    delay(500);
    turns(0.25);
    delay(200);
    turns(0.25);
    delay(200);
    turns(-1.0);
    delay(500);
  }
 
}

void turns(float rotations)
{
  // if the rotation count is -ve then it is CCW
  Serial.println();
  Serial.print("Turning : ");
  Serial.print(rotations);
  Serial.println(" rotations");
  bool clockwise = rotations > 0;
  Serial.print("Clockwise = ");
  Serial.println(clockwise);
  // calculate how many cycles the stepper will have to make
  int cycles = rotations * CYCLES_PER_ROTATION;
  // force the cycle count to be positive
  cycles = abs(cycles);
  Serial.print("That is ");
  Serial.print(cycles);
  Serial.print(" Cycles ");
  // only move if the user specifed an actual movement
  if(rotations != 0)
  {
    if (clockwise)
    {
      Serial.println("Clockwise");
      // for each cycle
      for (int x=0; x<cycles; x++)
      {
        // for each phase
        for(int y=0; y<8; y++)
        {
          // go to phase y
          phaseSelect(y);
          // pause so the stepper has time to react
          delay(1);
        }
      }
    } else {
      Serial.println("Counter Clockwise");
      // for each cycle
      for (int x=0; x<cycles; x++)
      {
        // for each phase (backwards for CCW rotation)
        for(int y=7; y>=0; y--)
        {
          // go to phase y
          phaseSelect(y);
          // pause so the stepper has time to react
          delay(1);
        }
      }
    }
  }
  // go to the default state (all poles off) when finished
  phaseSelect(8);
  Serial.println("Done");
}

void phaseSelect(int phase)
{
  switch(phase){
     case 0:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, HIGH);
       break; 
     case 1:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, HIGH);
       digitalWrite(IN4, HIGH);
       break; 
     case 2:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, HIGH);
       digitalWrite(IN4, LOW);
       break; 
     case 3:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, HIGH);
       digitalWrite(IN3, HIGH);
       digitalWrite(IN4, LOW);
       break; 
     case 4:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, HIGH);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, LOW);
       break; 
     case 5:
       digitalWrite(IN1, HIGH); 
       digitalWrite(IN2, HIGH);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, LOW);
       break; 
     case 6:
       digitalWrite(IN1, HIGH); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, LOW);
       break; 
     case 7:
       digitalWrite(IN1, HIGH); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, HIGH);
       break; 
     default:
       digitalWrite(IN1, LOW); 
       digitalWrite(IN2, LOW);
       digitalWrite(IN3, LOW);
       digitalWrite(IN4, LOW);
       break; 
  }
}

Have you tried the AccelStepper library?

Yes. I am learning it right now but I don't know how to make stepper motor go from point A to point B then go back etc.

#include <AccelStepper.h>

#define button1 5
#define button2 6

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(100);
  stepper.setAcceleration(20);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);

}

void loop()
{

  // here I don't know how to combine this code with one from my 1st post to make stepper move in my sequence by telling it to go e.g. +1.5 rotation --> wait 500ms --> then -0.5 rotation --> wait 200ms etc... 

}

And when I test AccelStepper Bounce example. It doesn't bounce. It goes in the same direction every time.

The simplest way is probably to use runToNewPosition() but it blocks the Arduino from doing anything else until the move has been completed.

You have not told us what stepper driver you are using. If it is one which takes step and direction signals you need to choose the DRIVER option when creating your instance of accelStepper.

...R