/* Rotary Encoder example code, modified to
 *  read 2 rotary encoders (A,B) and thier switches.
 *  https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/
*/
// Rotary Encoder Inputs
#define CLKA 2
#define DTA 3
#define SWA 4

#define CLKB 5
#define DTB 6
#define SWB 7

int Acounter = 0;
int Bcounter = 0;

int AcurrentStateCLK;
int BcurrentStateCLK;

int AlastStateCLK;
int BlastStateCLK;

String AcurrentDir ="";  //just easier than cleverly combining this string for seprate IO
String BcurrentDir ="";

unsigned long AlastButtonPress = 0;
unsigned long BlastButtonPress = 0;

/*Function Defs*/
void ReadA(int AcurrentStateCLK);
void ReadB(int BcurrentStateCLK);

void setup() {
  
  // Set encoder pins as inputs
  pinMode(CLKA,INPUT);
  pinMode(DTA,INPUT);
  pinMode(SWA, INPUT_PULLUP);
  pinMode(CLKB,INPUT);
  pinMode(DTB,INPUT);
  pinMode(SWB, INPUT_PULLUP);

  // Setup Serial Monitor
  Serial.begin(9600);

  // Read the initial state of CLK
  AlastStateCLK = digitalRead(CLKA);
  BlastStateCLK = digitalRead(CLKB);

}

void loop() {
  
  // Read the current state of CLK
  AcurrentStateCLK = digitalRead(CLKA);
  BcurrentStateCLK = digitalRead(CLKB);
  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (AcurrentStateCLK != AlastStateCLK  && AcurrentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DTA) != AcurrentStateCLK) {
      Acounter --;
      AcurrentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      Acounter ++;
      AcurrentDir ="CW";
    }

    Serial.print("A Direction: ");
    Serial.print(AcurrentDir);
    Serial.print(" | Counter: ");
    Serial.println(Acounter);
  }

  if (BcurrentStateCLK != BlastStateCLK  && BcurrentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DTB) != BcurrentStateCLK) {
      Bcounter --;
      BcurrentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      Bcounter ++;
      BcurrentDir ="CW";
    }

    Serial.print("B Direction: ");
    Serial.print(BcurrentDir);
    Serial.print(" | Counter: ");
    Serial.println(Bcounter);
  }

  // Remember last CLK state
  AlastStateCLK = AcurrentStateCLK;
  BlastStateCLK = BcurrentStateCLK;
  
  // Read the button state
  int AbtnState = digitalRead(SWA);
  int BbtnState = digitalRead(SWB);

  //If we detect LOW signal, button is pressed
  if (AbtnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - AlastButtonPress > 500) {
      Serial.println("Button A pressed!");
    }
  AlastButtonPress = millis();
  }
  if (BbtnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - BlastButtonPress > 50) {
      Serial.println("Button B pressed!");
    }
  
 // Remember last button press event
    BlastButtonPress = millis();
  }
  
 // Put in a slight delay to help debounce the reading
  delay(1);

} //end loop