Ok updated the code. Made it simplier and doesn't roll over the input from 4 to 1 on up or 1 to 4 on down. Now it goes up to 4 and stops so you have to turn it the other direction to turn it back to 1. I will just post it as text, it will lose all the formatting indents like good code so not as readable:
/**********************************************************************************
- This program controls the functions of relays on a PCBs to set Input Selection,
- The controller is a Arduino Nano and the pins relate to the processor named pins
- not the pin numbers of a 30 pin DIP device.
***********************************************************************************/
// Global variables
// Input Relay control output pin variables
const int Relay1=6, Relay2=7, Relay3=8, Relay4=9;
// LED control output pins variables
const int LED1=2, LED2=3, LED3=4, LED4=5;
// Volume Encoder D14/A0 and D15/A1
const int EncoderAPin=A0, EncoderBPin=A1;
// variables have to be volatile for use in the Interrupt Service Routine and in the Main Loop
volatile byte seqA = 0;
volatile byte seqB = 0;
volatile boolean InputUpInt = false;
volatile boolean InputDownInt = false;
int input=1;
// the setup function runs once when you press reset or power the board
void setup() {
//set input and LED pins to outputs to control relays
pinMode(LED1, OUTPUT); //pin D3 (see variable names above)
pinMode(LED2, OUTPUT); //pin D4
pinMode(LED3, OUTPUT); //pin D5
pinMode(LED4, OUTPUT); //pin D6
pinMode(Relay1, OUTPUT); //pin D7
pinMode(Relay2, OUTPUT); //pin D8
pinMode(Relay3, OUTPUT); //pin D9
pinMode(Relay4, OUTPUT); //pin D10
//set volume Encoder, input, and mute button pins to input
pinMode(EncoderAPin, INPUT); //pin D14
pinMode(EncoderBPin, INPUT); //pin D15
//set output pins for volume and mute to off, and input select relay to input 1
//Volume is in binary converted from Decimal, The input relay control is BCD
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
digitalWrite(Relay4, LOW);
digitalWrite(Relay4, LOW);
PCICR = 0b00000010; //PCIE1: pin Change interrupt Enable 1
PCMSK1 = 0b00000011; //Enable PIN Change Interrupt for A0 and A1
} //End of Setup
// Interrupt Service Routine
// Every detent in the rotary encoder generates 4 pin changes, 2 on Pin A and 2 on Pin B
ISR (PCINT1_vect){
//When Interrupt triggered read encoder pins
boolean A_val = digitalRead(EncoderAPin);
boolean B_val = digitalRead(EncoderBPin);
//Record Pin A and B sequences in the global variables
seqA <<= 1; //shift the current byte date over one bit to the left
seqA |= A_val; //OR in, add, the new bit value, 0 or 1, of the encoder pin A
seqB <<= 1; //shift the current byte date over one bit to the left
seqB |= B_val; //OR in, add, the new bit value, 0 or 1, of the encoder pin A
//Mask the Most Significant four bits
seqA &= 0b00001111; // wipes off the previous four reads that were moved
seqB &= 0b00001111; // to the four higher bits
//Compare the recorded sequence with the expected sequence
if (seqA == 0b00000011 && seqB == 0b00001001){
InputUpInt = true;
}
if (seqA == 0b00001001 && seqB == 0b00000011){
InputDownInt = true;
}
} // end ISR
void loop() {
//Booleans for task status, to be set true when a task is selected
bool inputUp=false, inputDown=false, task=false;
if(InputUpInt){
task=true;
InputUpInt=false;
if(input < 4){ // if not at 4 then add 1
input=input+1;
}
}//end of if(InputUpInt)
if(InputDownInt){
task=true;
InputDownInt=false;
if(input > 1){ // input not 1 then subtract 1
input=input-1;
}
}//end of if(inputDownInt)
//Perform the appropriate task based on input
if(task){
digitalWrite(LED1, LOW);
digitalWrite(Relay1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(Relay4, LOW);
switch(input){
case 1: digitalWrite(LED1, HIGH);
digitalWrite(Relay1, HIGH);
break;
case 2: digitalWrite(LED2, HIGH);
digitalWrite(Relay2, HIGH);
break;
case 3: digitalWrite(LED3, HIGH);
digitalWrite(Relay3, HIGH);
break;
case 4: digitalWrite(LED4, HIGH);
digitalWrite(Relay4, HIGH);
break;
default: break;
} // End Switch
} //End IF
// delay(5); // delay needed to smooth out control
} // End of Loop