#include <Wire.h>
#include <LiquidCrystal.h>
#include <RotaryEncoder.h>
#include <Adafruit_NeoPixel.h>

RotaryEncoder encoder(A2, A3, RotaryEncoder::LatchMode::TWO03);

int pin         =  3; 
int numPixels   = 16; // Popular NeoPixel ring size
#define MAX_SLIDE_POT_ANALOG_READ_VALUE 100

#define selectSW1 9
int mode;
const int modes = 4;
int analogSliderValues[modes];
static int pos = 50;
int newPos = 50;
static int pos1 = 50;
int newPos1 = 50;
static int pos2 = 50;
int newPos2 = 50;
static int pos3 = 50;
int newPos3 = 50;
int Mute = 0;

// setPos changes between 1 and 2 depending on whether line1() or line2() is called
byte setPos = 0;
int buttonState = 0;
int lastButtonState = 0;


const unsigned long shortPressThreshold = 100; // 500 ms for short press
const unsigned long longPressThreshold = 1000; // 2000 ms for long press
unsigned long pressDuration;
// Variables to keep track of button state and timing
bool buttonPressed = false;
unsigned long buttonPressTime = 0;
bool isToggled[modes] = {false, false, false, false}; // State for toggling newPos values

void setup() {
  pinMode(selectSW1, INPUT_PULLUP);
  Serial.begin(9600);

  // You may have to modify the next 2 lines if using other pins than A2 and A3
  PCICR |= (1 << PCIE1);                      // This enables Pin Change Interrupt 1 that covers the Analog input pins or Port C.
  PCMSK1 |= (1 << PCINT10) | (1 << PCINT11); 

  

}  // setup()



ISR(PCINT1_vect) {
  encoder.tick();  // just call tick() to check the state.
}  // ISR


// Read the current position of the encoder and print out when changed.
void loop() {
  readButton ();

  if (mode == 1) {
   
    Line1();
  }

   if (mode == 2)  {
    
    Line2();
  }
  if (mode == 3)  {
   
    Line3();
  }
  if (mode == 4)  {
    
    Line4();
  }
  String p1=":";
//Serial.println(newPos + p1 + newPos1 + p1 + newPos2 + p1 + newPos3 + p1 + mode);

analogSliderValues[0] = newPos;
analogSliderValues[1] = newPos1;
analogSliderValues[2] = newPos2;
analogSliderValues[3] = newPos3;





sendSliderValues();
printSliderValues();
}  // loop ()

void readButton() {
  if (digitalRead(selectSW1) == LOW) {
    if (!buttonPressed) {
      buttonPressed = true;
      buttonPressTime = millis(); // Record the time when button is pressed
    }
  } else {
    if (buttonPressed) {
      buttonPressed = false;
      pressDuration = millis() - buttonPressTime; // Calculate press duration

      if (pressDuration >= longPressThreshold) {
        handleLongPress();
      } else if (pressDuration >= shortPressThreshold) {
        handleShortPress();
      }
    }
  }
  delay(30);
}

void handleShortPress() {
  mode++;
  if (mode > modes) {
    mode = 1;
  }
}


void handleLongPress() {
  switch (mode) {
    case 1:
      newPos == 0;
      pos == 0;
      break;
    case 2:
      newPos1 == 0;
      break;
    case 3:
      newPos2 == 0;
      break;
    case 4:
      newPos3 == 0;
      break;
  }
}


/**** Print 1st setting on Line1 ***/
void Line1() {
  if (setPos != 1){
  setPos = 1;
  encoder.setPosition(pos);
  }
  newPos = encoder.getPosition();
  if (pos != newPos) {
    //printPos(newPos,0);
    //newPos = map(newPos, 0, 128, 0, 1024);
    pos = newPos;
  }
}  //Line1 ()


/**** Print 2nd setting on Line2 ***/
void Line2() {
 if (setPos != 2){
    setPos = 2;
    encoder.setPosition(pos1);
 }
  newPos1 = encoder.getPosition();
  if (pos1 != newPos1) {
    //printPos(newPos1,1);
    //newPos1 = map(newPos1, 0, 128, 0, 1024);
    pos1 = newPos1;

  }
}  //Line2 ()

void Line3() {
 if (setPos != 3){
    setPos = 3;
    encoder.setPosition(pos2);
 }
  newPos2 = encoder.getPosition();
  if (pos2 != newPos2) {
    //printPos(newPos1,1);
    //newPos2 = map(newPos2, 0, 128, 0, 1024);
    pos2 = newPos2;
  }
}  //Line2 ()

void Line4() {
 if (setPos != 4){
    setPos = 4;
    encoder.setPosition(pos3);
 }
  newPos3 = encoder.getPosition();
  if (pos3 != newPos3) {
    //printPos(newPos1,1);
    //newPos3 = map(newPos3, 0, 128, 0, 1024);
    pos3 = newPos3;
  }
}  //Line2 ()

void sendSliderValues() {
  String builtString = String("");

  for (int i = 0; i < mode; i++) {
    builtString += String((int)analogSliderValues[i]);

    if (i < mode - 1) {
      builtString += String("|");
    }
  }
  
  Serial.println(builtString);
}

void printSliderValues() {
  for (int i = 0; i < modes; i++) {
    String printedString = String("Slider #") + String(i + 1) + String(": ") + String(analogSliderValues[i]) + String(" mV");
    Serial.write(printedString.c_str());

    if (i < modes - 1) {
      Serial.write(" | ");
    } else {
      Serial.write("\n");
    }
  }
}