#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.
  }
}
uno:SCL
uno:SDA
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:14
uno:15
uno:16
uno:17
uno:18
uno:19
uno:20
uno:21
uno:5V.1
uno:5V.2
uno:22
uno:23
uno:24
uno:25
uno:26
uno:27
uno:28
uno:29
uno:30
uno:31
uno:32
uno:33
uno:34
uno:35
uno:36
uno:37
uno:38
uno:39
uno:40
uno:41
uno:42
uno:43
uno:44
uno:45
uno:46
uno:47
uno:48
uno:49
uno:50
uno:51
uno:52
uno:53
uno:GND.4
uno:GND.5
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
uno:A6
uno:A7
uno:A8
uno:A9
uno:A10
uno:A11
uno:A12
uno:A13
uno:A14
uno:A15
A4988
drv1:ENABLE
drv1:MS1
drv1:MS2
drv1:MS3
drv1:RESET
drv1:SLEEP
drv1:STEP
drv1:DIR
drv1:GND.1
drv1:VDD
drv1:1B
drv1:1A
drv1:2A
drv1:2B
drv1:GND.2
drv1:VMOT
stepper1:A-
stepper1:A+
stepper1:B+
stepper1:B-
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
r1:1
r1:2
pot1:GND
pot1:SIG
pot1:VCC
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r