Input and output pin changing and timing

Hello all, so I have keypad with 40 key's and 40 LED's To save cost I designed the circuit to use only 13 lines instead of 26. So if I want to turn on an LED I switch my columns to outputs, turn them high, and the row to low if that led needs to be on. To read a button I switch my Column Outputs to LOW and my rows to Inputs using the built in resistor. Input pullup. So the problem is I need to be able to scan all the inputs fast enough so the LEDs that need to be on don't appear to blink. However it seems that the delay in switching from an output to an input may be causing an issue, if I test the code to just control the LED's its fine, if I test the code for just the buttons its fine. If I run the code for the buttons, and the LED's if I press a button other LED's light up. If I put in long delays it does not happen. In my loop I call the buttons then the LED's Before you ask about the "verbose" code I have a couple problems that prevent that that I can get into later. If someone can give me an idea on the best way to do this I would appreciate it. Is there any way to define a pin as nothing? so it clears any previous state?

void led(){


  pinMode (6, OUTPUT);  
  pinMode (7, OUTPUT);  
  pinMode (8, OUTPUT);  
  pinMode (9, OUTPUT);  
  pinMode (10, OUTPUT);  
  pinMode (11, OUTPUT);  
  pinMode (12, OUTPUT);  
  pinMode (A1, OUTPUT);

 //SET 1 
   
  digitalWrite(6, !m[0]);
  digitalWrite(7, !m[1]);
  digitalWrite(8, !m[2]);
  digitalWrite(9, !m[3]);
  digitalWrite(12,!m[4]);
  digitalWrite(A1, !m[5]);
  digitalWrite(11, !m[6]);
  digitalWrite(10, !m[7]);
  digitalWrite((5, OUTPUT), HIGH);
  delay(2);
  pinMode (5, INPUT);


  //SET 2


  digitalWrite(8, !m[8]);
  digitalWrite(7, !m[9]);
  digitalWrite(10,!m[10]);
  digitalWrite(11,!m[11]);
  digitalWrite(A1,!m[12]);
  digitalWrite(9, !m[13]);
  digitalWrite(12,!m[14]);
  digitalWrite(6, !m[15]);
  digitalWrite((4, OUTPUT), HIGH);
  delay(2);
  pinMode (4, INPUT);  


  //SET 3

 
  digitalWrite(9, !m[16]);
  digitalWrite(8, !m[18]);
  digitalWrite(11, !m[20]);
  digitalWrite(10, !m[23]);
  digitalWrite(A1, !m[25]);
  digitalWrite(7, !m[27]);
  digitalWrite(12, !m[29]);
  digitalWrite(6, !m[31]);
  digitalWrite((3, OUTPUT),HIGH);
  delay(2);
  pinMode (3, INPUT);

  //SET 4

  digitalWrite(10, !m[17]);
  digitalWrite(6, !m[19]);
  digitalWrite(A1, !m[21]);
  digitalWrite(11, !m[22]);
  digitalWrite(7, !m[24]);
  digitalWrite(12, !m[26]);
  digitalWrite(8, !m[28]);
  digitalWrite(9, !m[30]);
  digitalWrite((2,OUTPUT), HIGH);
  delay(2);
  pinMode (2, INPUT);

  //SET 5 
  
  digitalWrite(10, !m[32]);
  digitalWrite(12, !m[33]);
  digitalWrite(9, !m[34]);
  digitalWrite(8, !m[35]);
  digitalWrite(7, !m[36]);
  digitalWrite(6, !m[37]);
  digitalWrite(11, !m[38]);
  digitalWrite(A1, !m[39]);
  digitalWrite((A0, OUTPUT), HIGH);
  delay(2);
  pinMode (A0, INPUT);



}
void buttons(){

  pinMode (8, INPUT_PULLUP);
  pinMode (12, INPUT_PULLUP);
  pinMode (11, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP); 
  pinMode (7, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);
  pinMode (10, INPUT_PULLUP);  
  pinMode (A1, INPUT_PULLUP);

  //********************************* Button Test ************************************
  //SET 1


  pinMode((5, OUTPUT), LOW);
  bitWrite (b[0],0,!digitalRead(6));
  bitWrite (b[0],1,!digitalRead(7));
  bitWrite (b[0],2,!digitalRead(8));
  bitWrite (b[0],3,!digitalRead(9));
  bitWrite (b[1],0,!digitalRead(12));
  bitWrite (b[1],1,!digitalRead(A1));
  bitWrite (b[1],2,!digitalRead(11));
  bitWrite (b[1],3,!digitalRead(10));
  pinMode(5, INPUT);
  delay(1);
  



  //_********************************************************** Button SET 2


  pinMode ((4, OUTPUT), LOW);
  bitWrite (b[2],0,!digitalRead(8));
  bitWrite (b[2],1,!digitalRead(7));
  bitWrite (b[2],2,!digitalRead(10));
  bitWrite (b[2],3,!digitalRead(11));
  bitWrite (b[3],0,!digitalRead(A1));
  bitWrite (b[3],1,!digitalRead(9));
  bitWrite (b[3],2,!digitalRead(12));
  bitWrite (b[3],3,!digitalRead(6));
  pinMode(4, INPUT);
  delay(1);
  

  //_********************************************************** Button SET 3

  pinMode ((3, OUTPUT), LOW);
  bitWrite (b[4],0,!digitalRead(9));
  bitWrite (b[4],1,!digitalRead(8));
  bitWrite (b[4],2,!digitalRead(11));
  bitWrite (b[4],3,!digitalRead(10));
  bitWrite (b[5],0,!digitalRead(A1));
  bitWrite (b[5],1,!digitalRead(7));
  bitWrite (b[5],2,!digitalRead(12));
  bitWrite (b[5],3,!digitalRead(6));
  pinMode (3, INPUT);
  delay(1);




  //_********************************************************** Button SET 4


  pinMode ((2, OUTPUT), LOW);
  bitWrite (b[6],0,!digitalRead(10));
  bitWrite (b[6],1,!digitalRead(6));
  bitWrite (b[6],2,!digitalRead(A1));
  bitWrite (b[6],3,!digitalRead(11));
  bitWrite (b[7],0,!digitalRead(7));
  bitWrite (b[7],1,!digitalRead(12));
  bitWrite (b[7],2,!digitalRead(8));
  bitWrite (b[7],3,!digitalRead(9));
  pinMode (2, INPUT);
  delay(1);

  


  //_********************************************************** Button SET 5


  pinMode ((A0, OUTPUT), LOW);
  bitWrite (b[8],0,!digitalRead(10));
  bitWrite (b[8],1,!digitalRead(12));
  bitWrite (b[8],2,!digitalRead(9));
  bitWrite (b[8],3,!digitalRead(8));
  bitWrite (b[9],0,!digitalRead(7));
  bitWrite (b[9],1,!digitalRead(6));
  bitWrite (b[9],2,!digitalRead(11));
  bitWrite (b[9],3,!digitalRead(A1));
  pinMode (A0, INPUT);
  delay(1);

Can you describe how this matrix is wired up in a bit more detail? At the moment it sounds almost as if you have each LED wired in parallel with its switch. Is that right? If so, where is the current-limiting resister relative to the switch?

Pin mode changes should not take any appreciable time and I don't believe that's your problem. However, these statements are nonsensical:

pinMode ((4, OUTPUT), LOW);

You'll need to look into Direct Port Manipulation.

Attached is a section of the schematic. I know I should post the whole deal but it really is identical all the way around. To answer your question yes the LED and the button are in parallel, with a diode to stop current flow.

circuit.jpg

That circuit looks reasonable, as long as your LEDs are capable of tolerating the reverse voltage you're applying.

Thank you for the input, so that leads me to believe that the LEDs lighting up when I press a button is due to the speed which I am scanning, and the input to output switching is not happening fast enough?