Problem in configuring button matrix

[Edit : Sorry I posted it in the wrong section, can someone move it to the multiplexing section?]

Hello,

I am using a Centipede shield with an Arduino Duemilanove and I adapted some code to suit the need to have 256 inputs. I have done a 16x16 matrix which works well when you press one key per time. I am getting troubles in managing multiple keypresses (I have some toggles in my circuit, not only momentary push buttons) . In particular if I keep one switch pushed then every time I push a second one I get output.

With reference to the code posted, for instance: If I press Row 0 Column 0 Switch I get D000: on the serial output and when I release the switch I get D001:, that’s ok.
If I keep Switch 0,0 pressed and I press Switch 7,0 I get :

D010:
D020:
D030:
D040:
D050:
D060:
D070:
D080:
D090:
D100:
D110:
D120:
D130:
D140:
D150:

I really don’t understand where the problem is, since I have

CS.digitalWrite(rows+i+offset,LOW);
pinStateSTR = String(CS.digitalRead(j+offset));
CS.digitalWrite(rows+i+offset,HIGH);

That should make me sure I am only reading one pin at a time…

Attached the code, maybe someone already experienced such problem.

Centipede CS;
int j1;
int pinState;
int pinNo; 
int doutpin;
String oldpinStateSTR, pinStateSTR, stringnewstate, stringoldstate;
int rows, columns, offset;
byte buttonBits;

void setup() 
{
  rows = 16;
  columns = 16;
  // Will need offset = 64 because index I2C Index 0 is already used by another device.
  offset = 64;
  
  Wire.begin(); // start I2C
 
  CS.initialize(); // set all registers to default
  // 256 Buttons
  stringoldstate = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
  
for (int k=0;k<rows;k++) // Setting sensing rows to input and enabling internal pull-up resistors.
  {
    CS.pinMode(k+offset,INPUT);
    CS.pinPullup(k+offset, HIGH);
  }

  for (int k=rows;k<columns+rows;k++) // Setting columns to HIGH.
  {
    CS.pinMode(k+offset,OUTPUT);
    CS.digitalWrite(k+offset,HIGH);
  }
  Serial.begin(115200);
  Serial.flush();
}

void loop() {
stringnewstate = "";
   for (int j=0;j<rows;j++) {
     for (int i=0;i<columns;i++) {
       CS.digitalWrite(rows+i+offset,LOW);
       pinStateSTR = String(CS.digitalRead(j+offset));
       CS.digitalWrite(rows+i+offset,HIGH);
       oldpinStateSTR = String(stringoldstate.charAt(i+j*columns));
       if (pinStateSTR != oldpinStateSTR) {
         Serial.print ("D");
         if (i+j*columns < 10) Serial.print("0");
         Serial.print (i+j*columns);
         Serial.print (pinStateSTR);
         Serial.println (":");
         }
         stringnewstate +=pinStateSTR;
       }
     }
   stringoldstate = stringnewstate;
}

Thanks to everyone in advance for any suggestion.

I am getting troubles in managing multiple keypresses (I have some toggles in my circuit, not only momentary push buttons) . In particular if I keep one switch pushed then every time I push a second one I get output.

Yes you will, that is how a matrix works. How have you wired it? Are there diodes at each matrix intersection?

No diodes, I guess that's the problem.. Would 1n4148 work?

Yes they will be fine.