//code adapted from 
//#define ENCODER_USE_INTERRUPTS
#include <Encoder.h>

//1st argument represents the A pin, 2nd represents the B pin
Encoder knobtrig(16, 17);
Encoder knobtime(18, 19);
Encoder knobvolt(20, 21);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600); 
}

//Sets encoder start values
long positiontrig  = 5;
long positiontime = 5;
long positionvolt = 5;
long newTrig = 5;
long newTime = 5;
long newVolt = 5;


void loop() {
  //uses polling to read in encoder values as long as they are within 0 and 9
  //
  cli();
  if (knobtrig.read() < 0){
    knobtrig.write(0);
  }
  if (knobtrig.read() > 9){
    knobtrig.write(9);
  }
  newTrig = knobtrig.read();
  
  if (knobtime.read() < 0){
    knobtime.write(0);
  }
  if (knobtime.read() > 9){
    knobtime.write(9);
  }
  newTime = knobtime.read();

  if (knobvolt.read() > 9){
    knobvolt.write(9);
  }
  if (knobvolt.read() < 0){
    knobvolt.write(0);
  }
  newVolt = knobvolt.read();

//  sei();
  //if value of encoder has changed, check and make sure encoder readings didn't break the bounds 0 or 9
  if (newTrig != positiontrig || newTime != positiontime || newVolt != positionvolt) {
    if (newTrig == -1){
      newTrig = 0;
    }
    if (newTrig == 10){
      newTrig = 9;
    }
    if (newTime == -1){
      newTime = 0;
    }
    if (newTime == 10){
      newTime = 9;
    }
    if (newVolt == -1){
      newVolt = 0;
    }
    if (newVolt == 10){
      newVolt = 9;
    }
    //if values of encoder has changed, print the new value
    Serial.print("trig = ");
    Serial.print(newTrig);
    Serial.print(", time = ");
    Serial.print(newTime);
    Serial.print(", volt = ");
    Serial.print(newVolt);
    Serial.println();
    positionvolt = newVolt;
    positiontime = newTime;
    positiontrig = newTrig;
  }
  sei();
}
