// Franken Mill 1.13.4
#include <Stepper.h>

// Rotary Encoder Inputs_Step control, hand-wheel control
#define CLK 2
#define DT 3
// Rotary Encoder Inputs_ multiplier
#define encoderOutA 5 // CLK
#define encoderOutB 6 // DT
#define SW 4
int vcounter = 10; // Set initial base multiplier to 10
int State;
int old_State;
int speedset; //speed adjust
//////////////////////////

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
unsigned long lastButtonPress = 0;

//Steppers

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 9, 10);

int lastcounter = 0;

void setup() {
  {
    pinMode (encoderOutA, INPUT);
    pinMode (encoderOutB, INPUT);
    Serial.begin (115200);
    //Read First Position of Channel A
    old_State = digitalRead(encoderOutA);
  }

  {
    // Set encoder pins as inputs
    pinMode(CLK, INPUT);
    pinMode(DT, INPUT);
    pinMode(SW, INPUT_PULLUP);
    // Read the initial state of CLK
    lastStateCLK = digitalRead(CLK);

    // set the speed:
    myStepper.setSpeed(1250);
    // initialize the serial port:
    Serial.begin(115200);
  }
}
void loop() {
  {
    State = digitalRead(encoderOutA);
    if (State != old_State)
    {
      if (digitalRead(encoderOutB) != State)
      {
        vcounter ++;
      }
      else {
        vcounter --;
      }
      Serial.begin(115200);
      Serial.print("Speed: ");
      Serial.println(vcounter);
    }
    old_State = State; // Check for change of state
  }

  {
    // Read the current state of CLK
    currentStateCLK = digitalRead(CLK);

    // If  state of CLK are different, then pulse occurred
    // React to first state change 
    if (currentStateCLK != lastStateCLK  && currentStateCLK == 1) {

      // If the DT state is different than the CLK state then
      // the encoder is rotating CCW so decrement
      if (digitalRead(DT) != currentStateCLK) {
        counter --;
        currentDir = "CCW";
      } else {
        // Encoder is rotating CW so increment
        counter ++;
        currentDir = "CW";
      }
      Serial.begin(115200);
      Serial.print("Direction: ");
      Serial.print(currentDir);
      Serial.print(" | Counter: ");
      Serial.println(counter);
      if (currentDir.equals("CCW")) {
        Serial.println("counterclockwise");
        //myStepper.step(-stepsPerRevolution);
        myStepper.step(((counter - lastcounter)*vcounter) * 5);
      } else if (currentDir.equals("CW")) {
        Serial.println("clockwise");
        myStepper.step(((counter - lastcounter)*vcounter) * 5);
      }
    }

    // Remember last CLK state
    lastStateCLK = currentStateCLK;
    // Remember last counter value
    lastcounter = counter;

    // Read the button state
    int btnState = digitalRead(SW);

    //If we detect LOW signal, button is pressed
    if (btnState == LOW) {
      //if 50ms have passed since last LOW pulse, it means that the
      //button has been pressed, released and pressed again
      if (millis() - lastButtonPress > 50) {
        Serial.println("Button pressed! Reset Speed to 10");
        vcounter = 10;
      }

      // Remember last button press event
      lastButtonPress = millis();
    }
  }
}
