Hi VirtualDDS,
I'm no C++ programmer so am little help on the code but may have a suggestion.
If the encoder your planning to use is a position encoder then could you ensure it's inputs are all on the same mega port and read the entire port instead of one pin at a time. Then you would just read the port, mask off unwanted pins and use a switch/case to select what to do depending on result.
If you cannot put entire encoder on one port then maybe creating an array of pins to read and in a loop read each pins state and rotating it's result into a byte or int variable. At the end of the loop you would use switch/case to act on result.
// List of input pins to read
const int Input_Pins [7] = {
1,5,3,7,9,11,2};
void setup(){
// set list of pins to input
for (int a=0;a<7;a++){
pinMode(Input_Pins[a],INPUT);
}
}
void loop(){
// Read the pins
int x = readPins();
// Act on returned value
switch (x) {
case 0: // 12 O'Clock
// statements
break;
case 90: // 3 O'Clock
// statements
break;
case 180: // 6 O'Clock
// statements
break;
case 270: // 9 O'Clock
// statements
break;
default: // Any other value, do nothing
// statements
break;
}
}
int readPins(){
int result = 0; // Setup result register
for (int a=0;a<7;a++){ // Index through array
int x=digitalRead(Input_Pins[a]); // Read pin state
result=result << 1; // Rotate result register
result = result | x; // OR pinstate into bit 0
}
return result; // Return
}