#define POT A14

#define RESET A0
#define STEP A1             
                            //Define pins.
#define ccw A6
#define cw A7

#define stepPin 4
#define dirPin 5


int rotation = 1;      //Initialize variables.
int stepCount = 1;

int customDelay, customDelayMapped;  //These variables are used for the calculation of the 
                                     //stepper motor speed.

int speedUp() {   //This function is for the calculation of the stepper motor speed.
  int customDelay = analogRead(POT); 
  int newCustom = map(customDelay, 0, 1023, 4000,300); 
  return newCustom;    
}

void setup() {
  // put your setup code here, to run once:
  pinMode(POT, INPUT);
  pinMode(RESET, INPUT);
  pinMode(STEP, INPUT);
  pinMode(ccw, INPUT);

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

  // Serial.begin(9600);
  
   digitalWrite(dirPin,HIGH); //To set the stepper motor rotating motion to clockwise.

  int a = 0;
  do{
    int a = analogRead(POT);
    if(a > 0){         //This part of code is to avoid a bug where the stepper doesn't
                       //work properly.
      loop();
    }
  }while(a == 0);

  /*

  Yellow Button is for Reset
  Blue Button is for Step
  Left Green Button is for Counter-Clockwise
  Right Green Button is for Clockwise
  
  */
}

void loop() {
  // put your main code here, to run repeatedly:
  int motorSpeed = analogRead(POT);

  int resetButton = digitalRead(RESET);
  int stepButton = digitalRead(STEP);

  int cwValue = digitalRead(cw);
  int ccwValue = digitalRead(ccw);
  

  if(motorSpeed != 0){
    customDelayMapped = speedUp();
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelayMapped);  //This part of code is to avoid a bug where
    digitalWrite(stepPin, LOW);            //the stepper motor doesn't work properly.
    delayMicroseconds(customDelayMapped);
  }

  if(ccwValue == 0 && cwValue == 1){
    if(rotation != -1){
      rotation = -1;                        //counter-clockwise
      stepCount = 200 - stepCount;   
      digitalWrite(dirPin, LOW);
    }
                                          
  }else if(ccwValue == 1 & cwValue == 0){
    if(rotation != 1){
      rotation = 1;                          //clockwise            
      stepCount = 200 - stepCount;
      digitalWrite(dirPin, HIGH);
    }
    
  }

  if(resetButton == 0){
    if(rotation == 1){
      digitalWrite(dirPin, LOW);
      for(int a = 0; a < stepCount; a++){
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);                 
        digitalWrite(stepPin, LOW);   
        delayMicroseconds(500);
      }                                         //To reset the stepper motor.
      stepCount = stepCount - stepCount;
      //stepCount = stepCount - 1;  
      digitalWrite(dirPin, HIGH);
      delay(500);
    }else{
      digitalWrite(dirPin, HIGH);
      for(int a = 0; a < stepCount; a++){
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);           
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
      }                                        //To reset the stepper motor.
      stepCount = stepCount - stepCount;
      //stepCount = stepCount - 1;       
      digitalWrite(dirPin, LOW);
      delay(500);
    }
   }
   // Serial.println(stepCount);
  if(stepButton == 0){
    if(rotation == 1){
      digitalWrite(dirPin, LOW);
      for(int a = 0; a < stepCount; a++){
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);     //To reset the stepper motor first.
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
      }
      digitalWrite(dirPin, HIGH);
      for(int a = 0; a < 553; a++){
        digitalWrite(stepPin, HIGH); //To set the specific angle.
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000);
      }
      stepCount = 153;
      delay(500);
    }else{
      digitalWrite(dirPin, HIGH);
      for(int a = 0; a < stepCount; a++){
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);      //To reset the stepper motor first.
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
      }
      digitalWrite(dirPin, LOW);
      for(int a = 0; a < 585; a++){
        digitalWrite(stepPin, HIGH);   //To set the specific angle.
        digitalWrite(stepPin, LOW);
        delayMicroseconds(1000);
      }
      stepCount = 185;
      delay(500);
    }
  }
 
  stepCount++; 

  if(stepCount > 200){   //If the stepCount value is greater than 200,
    stepCount = 1;       //then its value will be 1.
  }
}
A4988