how to define key presses with i2c keypad

Hi Guys,

Can someone direct me to a tute or help with how to assign certain characters to a key press?

My problem is; I have a 16 key web4robot i2c lcd & keypad, it assigns values to keys pressed different to that of the keypad, as below;
Button 1 = 1

     Button 1 = 1
     Button 2 = 4
     Button 3 = 7
     Button 4 = *
     Button 5 = 2
     Button 6 = 5
     Button 7 = 8
     Button 8 = 3
     Button 9 = 6
     Button 10= 9
     Button 11= #
     Button 12= A
     Button 13= B
     Button 14= C
     Button 15= D
     Button 16= E

I've found the keyMap tute's but they reference pins and not i2c, how do would I define the above keypress values to characters.
I'm using the LCDi2cW.h library if that helps.

TIA.

Look at the Switch function:

bugger. I was hoping for something like;
#define keypress 1 = 1;

or something to that effect.

Can the switch case be put in setup?

Sure, remember setup() runs once.

I don't see how I2C comes into it. Anyway, an array?

char buttons [16] = { '1', '4', '7', '*',  };

And so on after the '*'.

Thanks Nick, will try that tonight.
I asked about the i2c because what I understood from the keypad tute was that you assigned the keypad value via the pins, with communication from LCD and keypad coming in on 2 wires I did not understand how that would affect the the keypad value or how to assign a particular value to the key press.
I’ll work through this and hopefully get a better grasp of it and arrays.

Not a lot of love there unfortunately.
I applied a number of variations of this to no avail.

#include <Wire.h>
#include <LCDi2cW.h>                    
LCDi2cW lcd = LCDi2cW(4,20,0x4C,0);   
char buttons[17] = { '1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };

void setup()
{ 
  lcd.init(); 
  lcd.setCursor(1,0);
  lcd.print("Keypad = ");
}

void loop()
{
  buttons[17]=lcd.keypad();
  lcd.setCursor(1,10);        
  lcd.print("   "); 
  lcd.setCursor(1,10);
  lcd.print(buttons[17],DEC);      
  delay(100);  
}

What I don’t understand is, by using an array but not linking the particular keypress results to the array, how would the arduino know to associate the relevant keypress against the array?
TIA.

LCDi2cW::keypad() returns a value between 0 and 16:

byte digit = lcd.keypad

You can use this result as the index of your array to get the associated character:

char displayChar = buttons[digit];

At this point, you can display the appropriate character on the LCD:

lcd.print(displayChar);

You'll have to keep in mind, though, that arrays are indexed from 0 to n-1 where n is the size of the array. In your sample code, you declare the array to have 17 elements, only give it 16, and then try to access the 17th element which isn't valid (as it only goes from 0 - 16). You'll probably want to put a filler character in the first element of the array, and only print something if it's not 0:

if (digit > 0)
{
  lcd.print(displayChar);
}

Thanks Arrch.
I must have misunderstood the array tute, I was thinking it had to be the number of inputs + 1 to include a null, which is why I changed the 16 mentioned earlier by Nick.
I won't get a chance to try this for a couple of days but keen to see if this solves it.
Thanks for taking the time to explain.

Hi again,

I’ve tried applying what I thought I understood in a few ways but have not had any success, it’s probably a fair assumption I have misunderstood something.
The sketch I’m most comfortable with is this;

  #include <Wire.h>
  #include <LCDi2cW.h>                    
  LCDi2cW lcd = LCDi2cW(4,20,0x4C,0); 
  char buttons[16] = { '1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };
      
  void setup()
  { 
    lcd.init(); 
  }
  
  void loop()
  {
    char displayChar = buttons[16];
    byte digit = lcd.keypad();
    lcd.clear();
    lcd.setCursor(1,10);
    if (digit > 0)
    {
      lcd.print(displayChar,DEC);
      delay (1000);
    }
  }

however it only returns a value of 4, being the 4 from the reference… LCDi2cW lcd = LCDi2cW(4,20,0x4C,0);

can someone point out where I’ve gone wrong please?

Thanks,
Paul.

char displayChar = buttons[16];

You've declared displayChar to be an array with 16 elements, so why are you trying to get the 17th element? I guess you missed my explanation on array indexes.

Notice the difference between the code that I posted:
char displayChar = buttons[digit];
and yours:

char displayChar = buttons[16];

?

I understood (I think so at least) the array elements being n-1, (n=16 “1-D”, n-1 =15) but when I applied it as;

  char buttons[15] = { '1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };

I got the error “error: too many initializers for ‘char [15]’”.

my current sketch is this;

  #include <Wire.h>
  #include <LCDi2cW.h>                    
  LCDi2cW lcd = LCDi2cW(4,20,0x4C,0); 
  char buttons[15] = { '1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };
      
  void setup()
  { 
    lcd.init(); 
  }
  
  void loop()
  {
    byte digit = lcd.keypad();
    char displayChar = buttons[digit];
    lcd.clear();
    lcd.setCursor(1,10);
    if (digit > 0)
    {
      lcd.print(displayChar,DEC);
      delay (1000);
    }
  }

which won’t compile, if I use 16 in lieu of 15, it compiles but returns numbers in the 50-60 range.

I corrected the DEC reference and that has more or less given me sensible results, they just don’t match up in the order as listed in the array, I think I can arrange them to suit.

  #include <Wire.h>
  #include <LCDi2cW.h>                    
  LCDi2cW lcd = LCDi2cW(4,20,0x4C,0); 
  char buttons[16] = { '1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };
      
  void setup()
  { 
    lcd.init(); 
  }
  
  void loop()
  {
    byte digit = lcd.keypad();
    char displayChar = buttons[digit];
    lcd.clear();
    lcd.setCursor(1,10);
    if (digit > 0)
    {
      lcd.print(displayChar);
      delay (1000);
    }
  }

Ok, I’m pretty sure I don’t understand why this worked, but it worked.

  #include <Wire.h>
  #include <LCDi2cW.h>                    
  LCDi2cW lcd = LCDi2cW(4,20,0x4C,0); 
  char buttons[17] = {'0','1', '4', '7', '*', '2', '5','8','0','3','6','9','#','A','B','C','D' };
      
  void setup()
  { 
    lcd.init(); 
  }
  
  void loop()
  {
    byte digit = lcd.keypad();
    char displayChar = buttons[digit];
    lcd.clear();
    lcd.setCursor(1,10);
    if (digit > 0)
    {
      lcd.print(displayChar);
      delay (1000);
    }
  }

when I use the keypad example from LCDi2cW, the keypress’s return values from 1-16, for whatever reason, on the array used above, LCDi2cW returns the value at n+1.
At least that’s how I’m interpreting it, and I don’t know why.
In any case, the above sketch works like a charm, many thanks Arrch, Larry and Nick for your time.