48 pot MIDI controller needs to be faster...

Ok, everything is working pretty smoothly now except for one hiccup. Instead of the first pot of each of the 3 groups of 16 pots sending data as controller 0, it is sending as controller 1 and the last pot of the 16 pot group is sending as 0. In the second and third groups of 16 the last pot is not being read at all and the first pot of the second group is sending data as 16 and 17 at the same time. I have a feeling this has something to do with my crazy array of PORT values, but I'm not seeing it.
Here's the code, if anyone else sees what I am probably obviously doing wrong please lemme know. Thank you:

byte input[48] = {
B00000000,
B00000001,
B00000010,
B00000011,
B00000100,
B00000101,
B00000110,
B00000111,
B00001000,
B00001001,
B00001010,
B00001011,
B00001100,
B00001101,
B00001110,
B00001111,
B00000000,
B00010000,
B00100000,
B00110000,
B01000000,
B01010000,
B01100000,
B01110000,
B10000000,
B10010000,
B10100000,
B10110000,
B11000000,
B11010000,
B11100000,
B11110000,
B00000000,
B00000001,
B00000010,
B00000011,
B00000100,
B00000101,
B00000110,
B00000111,
B00001000,
B00001001,
B00001010,
B00001011,
B00001100,
B00001101,
B00001110,
B00001111};  //set array of port values, 0-15 are PORTB pins 8-11, 16-31 are PORTD pins 4-7, 32-47 are PORTC pins 0-1 and PORTD pins 2-3

//byte controller[48] = {64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111};  //MIDI controller number to send
int chanVal[48]; //initialize array to hold all 48 values to compare with old values
int oldChanVal[48];  //initialize array to hold all 48 old values to compare with new values

void setup(){

DDRB = B00001111;  // set PORTB (digital pins 8-11) to output
PORTB = B00000000;  // set PORTB to LOW
DDRD = DDRD | B11111100;  // set PORTD (digital pins 2-7) to output while making sure not to change pins 0 and 1 (TX and RX)
PORTD = B00000000;  // set PORTD to LOW
DDRC = DDRC | B000011;  // set PORTC (analog pins 0-1) to output 
PORTC = B000000;  // set PORTC to LOW
Serial.begin(38400);  //set this to 31250 for MIDI
}

void loop(){

for(int a=0; a < 48; a++){  //loop through all 48 channels
  
  if((a >= 0) && (a < 16)){  //during first 16 channels change PORTB value and assign analog value to chanVal array
    chanVal[a] = map(analogRead(A2), 0, 1023, 0, 127);  
    PORTB = input[a];
   }
  
  else if((a >= 16) && (a < 32)){  //during 2nd 16 channels change PORTD value and assign analog value to chanVal array
    chanVal[a] = map(analogRead(A3), 0, 1023, 0, 127);  
    PORTD = input[a];
   }
  
  else if((a >= 32) && (a < 48)){  //during 3rd 16 channels change PORTD and PORTC value and assign analog value to chanVal array
    chanVal[a] = map(analogRead(A4), 0, 1023, 0, 127);  
    PORTD = input[a]; 
    PORTC = input[a];
   }
  
  if(abs(chanVal[a] - oldChanVal[a]) > 1){  //test to see if any of the values in the chanVal array have changed by more than one -- if so, print
    Serial.print(0xB0);
    Serial.print("::");
    Serial.print(a);
    Serial.print("::");
    Serial.println(chanVal[a]);
  
}
  
oldChanVal[a] = chanVal[a]; //assign chanVal values to oldChanVal before starting over  
}

delay(48);  //less than about 48ms causes slow knob movements not to be picked up well and more than 48 causes latency issues
}

And when I isolate any of the if statements by commenting out the other two -- the isolated one will act just like the first group of 16 with the last pot being read as 0.