Go Down

Topic: Possible problem with I2CKeypad library (Read 500 times) previous topic - next topic

pgmartin

May 29, 2012, 05:55 pm Last Edit: May 30, 2012, 12:47 am by pgmartin Reason: 1
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: [Select]

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

johnwasser

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

pgmartin

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

Go Up