How to change setHoldTime from Keypad.h in "Multikey.ino"

Hi.
Can someone please help me. Excuse my ignorance with calling up libraries and changing variable in them.

I have a 4 x 3 keypad and have loaded "multikey.ino" from the "keypad" library. This calls up "keypad.h". In there is a setting "setHoldTime(uint)

In the corresponding keypad.cpp file there is a line:

setHoldTime(500);

I want to change the hold time to 1000. Is there a way I can do this from within the multikey.ino file? I tried the following:

Keypad.setHoldTime(1000);

and also,

setHoldTime(unsigned int 1000);

Neither work. Can someone please help.

Keypad is a class and the setHoldTime is declared as public, so accessible on any instance of that class.

when you try to do

[b]K[/b]eypad.setHoldTime(1000);

you are trying to call a class method (because of the capital K) which does not work. what you need to do is call the method after initialization of your instance of the keypad and for your specific instance

in your program you will create an instance of the Keypad class with something like

const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[rows] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

and then in the setup function you can call on your own instance myKeypad the setHoldTime method

myKeypad.setHoldTime(1000);

this will set the Hold time for your specific instance of Keypad to 1000

makes sense?

[quote author=J-M-L link=msg=2806841 date=1466333763]
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

Yes that worked.
I found there was a line similar to the one you wrote above. This one is:

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Unfortunately I don't really understand what a "class" is and declaring something as public. Can you explain it in simpler terms?

OK - trying to explain this in simple terms - not 100% accurate or complete but will get the point across:

A class is a template, something that defines a concept. here for example the concept of a Keypad.
An instance is an object in real life. like your physical keypad you touch, in your experiment

When you use the library and call

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

you are instantiating the class to get an object you can then manipulate. if you had two keyPads, you would then create a second instance with a different name and each object (or instance) would have it's own attributes (instance variables). For example which pins they are connected to, which keys are represented, the number of rows and cols.

when you develop a class, you can define public methods (public = they can be called from outside the class) that applies to an instance of that class (the physical object). For example if you had two keyPads, you could set the HoldTime to 100 for one and to 5000 for the other one. The call applies to your specific object. You have two instances of the same conceptual object (a Keypad) but they have slight differences.

but when you develop a class you can also provide private methods, those are utilities that only the class can use. And in some programming languages, you can also provide class methods which would affect then every single instance of the class.

You can read more information as an introduction in the Writing a Library for Arduino article

JML,

thanks for that explanation. I understand it in principle, but it is like a different language. I read the example you suggested in link, which explained the creation of a library. I could now repeat that exercise, but without too much knowledge of "why".

But I have now learnt about calling functions using the prefix as you mentioned earlier. I now also understand a bit better how to distinguish the public and private variables (usually and _ before the name) - in the library.

If you are begining, don't worry too much about creating your own classes. just understand that when you write something like

Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

you are asking the [b]K[/b]eypad class to create an instance of itself by calling what we call a constructor .

The constructor is the makeKeymap function to which you give some parameters that will be used when instantiating your own object.

Once this is done, all the work you do on your object is done by calling method on your instance in the form of

myKeypad.functionToCall(parameters);

functionToCall has to be public - something documented in the .h of the class definition.