Pages: [1]   Go Down
Author Topic: Possible problem with I2CKeypad library  (Read 376 times)
0 Members and 1 Guest are viewing this topic.
Chile
Offline Offline
Sr. Member
****
Karma: 0
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was reading a post (http://arduino.cc/forum/index.php/topic,95027.0.html) about a bug in the keypad library, that might cause a short if multiple keys are pressed at the same time. I use keypads regularly in my projects, directly and with an I2C port expander, so I took a look at the i2ckeypad library (http://arduino.cc/playground/Main/I2CPortExpanderAndKeypads) to verify if it can suffer the same problem.

Looking at the code seems that the same problem may happen: if you press two keys in one row, a short between a column that is HIGH and the other LOW will happen. Am I right?

Is there a way to avoid this problem via software or should I install 220 Ohm resistors in series with each column to avoid big currents?

By the way, it looks like the get_key() method is scanning only one row at the time. Is that right?

Code:
char i2ckeypad::get_key()
{
  static int temp_key;

  int tmp_data;
  int r;

  int key = '\0';

  // Search row low
  pcf8574_write(pcf8574_i2c_addr, pcf8574_row_data[row_select]);

  for(r=0;r<num_cols;r++) {
    // Read pcf8574 port data
    tmp_data = pcf8574_byte_read(pcf8574_i2c_addr);

    // XOR to compare obtained data and current data and know
    // if some column are low
    tmp_data ^= current_data;

    // Key pressed!
    if(col[r] == tmp_data) {
      temp_key = keymap[row_select][r];
      return '\0';
    }
  }

  // Key was pressed and then released
  if((key == '\0') && (temp_key != '\0'))   
  {
    key = temp_key;
    temp_key = '\0';
    return key;
  }

  // All PCF8574 ports high again
  pcf8574_write(pcf8574_i2c_addr, 0xff);

  // Next row
  row_select++;
  if(row_select == num_rows) {
    row_select = 0;
  }

  return key;
}

Regards
« Last Edit: May 29, 2012, 05:47:26 pm by pgmartin » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8084
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adding resistors sounds like a reasonable precaution when a HIGH output could be shorted to a LOW output.

I don't think you can fix the problem in software without adding hardware like pull-up or pull-down resistors.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Chile
Offline Offline
Sr. Member
****
Karma: 0
Posts: 260
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks johnwasser. I'll add resistors. I also added a note in the playground wiki article.
Logged

Pages: [1]   Go Up
Jump to: