arduino中Keypad 库函数介绍

原文:https://playground.arduino.cc/Code/Keypad/

  • Creation

    构造函数:

    1. Keypad(makeKeymap(userKeymap), row[], col[], rows, cols)
    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 keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
    

    实例化一个键盘对象,该对象使用引脚5、4、3、2作为行引脚,并使用8、7、6作为列引脚。
    该键盘有4行3列,产生12个键。


    Functions

    void begin(makeKeymap(userKeymap))

    初始化内部键盘映射使其等于userKeymap
    [请参见文件->示例->键盘->示例-> CustomKeypad ]

    char waitForKey()

    此功能将永远等待,直到有人按下某个键。**警告:**它会阻止所有其他代码,直到按下某个键为止。这意味着没有闪烁的LED,没有LCD屏幕更新,除了中断例程外什么也没有。

    char getKey()

    返回按下的键(如果有)。此功能是非阻塞的。

    KeyState getState()

    返回任何键的当前状态。
    四个状态为“空闲”,“已按下”,“已释放”和“保持”。

    boolean keyStateChanged()

    New in version 2.0: Let’s you know when the key has changed from one state to another. For example, instead of just testing for a valid key you can test for when a key was pressed.

    2.0版的新功能:让我们知道密钥何时从一种状态更改为另一种状态。例如,您不仅可以测试有效的按键,还可以测试按键的按下时间。

    setHoldTime(unsigned int time)

    Set the amount of milliseconds the user will have to hold a button until the HOLD state is triggered.

    设置用户必须按住按钮直到触发HOLD状态的毫秒数。

    setDebounceTime(unsigned int time)

    Set the amount of milliseconds the keypad will wait until it accepts a new keypress/keyEvent. This is the “time delay” debounce method.

    设置键盘将等待直到接受新的keypress / keyEvent的毫秒数。这是使用“时间延迟”防止抖动方法。

    addEventListener(keypadEvent)

    Trigger an event if the keypad is used. You can load an example in the Arduino IDE.
    [See File -> Examples -> Keypad -> Examples -> EventSerialKeypad] or see the KeypadEvent Example code.

    如果使用键盘,则触发事件。您可以在Arduino IDE中加载示例。
    [请参阅文件->示例->键盘->示例-> EventSerialKeypad ]或查看KeypadEvent示例代码。

    For Now

    Here’s the list of multi-keypress functions and the keylist definition. I will complete their descriptions this weekend.

    • Key key[LIST_MAX]
    • bool getKeys()
    • bool isPressed(char keyChar)
    • int findInList(char keyChar)

    Example

    #include <Keypad.h>

    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 keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

    void setup(){
    Serial.begin(9600);
    }

    void loop(){
    char key = keypad.getKey();

    if (key != NO_KEY){
    Serial.println(key);
    }
    }

    [Get Code]


    FAQ

    • How do I use multiple Keypads?

    Keypad is a class. Therefore to use multiple Keypad, you must create an instance for each of them. In the example above, the Keypad instance keypad) was bound to the digital pins 2, 3, 4, 5, 6, 7 and 8.

    To add a Keypad bound to digital pins 9, 10, 11, 12, 13, 14, 15 and 16, you could create the following instance keypad2:

    Keypad是一个类。因此,要使用多个键盘,必须为每个键盘创建一个实例。在上面的示例中,小键盘实例小键盘)已绑定到数字引脚2、3、4、5、6、7和8。

    要添加绑定到数字引脚9、10、11、12、13、14、15和16的键盘,可以创建以下实例keyboard2

    const byte ROWS2 = 4; //four rows
    const byte COLS2 = 4; //four columns
    char keys2[ROWS2][COLS2] = {
      {'.','a','d','1'},
      {'g','j','m','2'},
      {'p','t','w','3'},
      {'*',' ','#','4'}
    };
    byte rowPins2[ROWS2] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
    byte colPins2[COLS2] = {16, 15, 14, 13}; //connect to the column pinouts of the keypad
    
    Keypad keypad2 = Keypad( makeKeymap(keys2), rowPins2, colPins2, ROWS2, COLS2 );
    

    And now it’s just a matter of using whatever function is wanted on each keypad:

    现在,只需使用每个键盘上需要的任何功能即可:

    //update instances and possibly fire funcitons
    void loop(){
      char key1 = keypad.getKey();
      char key2 = keypad2.getKey();
    
      if (key1 != NO_KEY || key2 != NO_KEY){
        Serial.print("You pressed: ");
        Serial.print(key1 != NO_KEY ? key1 : "nothing on keypad");
    	Serial.print(" and ");
        Serial.print(key2 != NO_KEY ? key2 : "nothing on keypad2");
        Serial.println(".");
      }
    }
    
    • How do I use setDebounceTime(unsigned int time)?

    在Arduino中,按照File-> Examples-> Keypad-> Examples-> DynamicKeypad的主菜单进行操作。打开草图后,找到setup(),您将看到:

void setup(){ 
  Serial.begin(9600; 
  digitalWrite(ledPin,HIGH); //打开LED。
  keyboard.addEventListener(keypadEvent); //添加事件监听器。
  keyboard.setHoldTime(500; //默认值是1000mS 
  keyboard.setDebounceTime(250; //默认值为50mS 
}

这表明去抖时间将允许每250毫秒按一次键。如果在该时间范围内发生了多次按键操作(如按键弹起时会发生这种情况),那么这些多余的按键操作将被忽略。

引用\[1\]:在Arduino,您可以使用键盘库来读取矩阵类型的键盘。您可以按照File->Examples->Keypad->Examples->DynamicKeypad的主菜单进行操作。打开草图后,您将看到setup()函数的一些代码,其包括初始化串口、打开LED以及设置事件监听器和延迟时间等。\[1\] 引用\[2\]:键盘库允许您使用Arduino读取矩阵类型的键盘。这些键盘可以从旧电话清除,也可以从电子零件商店以低廉的价格购买。它们具有不同的配置和标记,例如3x4、4x4等,并且可以支持各种键盘布局。您可以从https://playground.arduino.cc/Main/KeypadTutorial/下载最新版本的键盘库,其包括四个示例草图。\[2\] 引用\[3\]:键盘库支持用户定义的引脚和键映射,因此您无需更改库文件。如果您确实需要更改库文件,您需要将文件保存到正确的文件夹(\ $ ArduinoHome $ \ libraries \)。在保存后,您可能需要退出并重新启动Arduino IDE软件,以便识别库文件夹的任何新文件。您可以在以下链接找到更多关于使用和创建库的信息:http://www.arduino.cc/en/Main/Libraries、http://www.arduino.cc/en/Hacking/Libraries、http://www.arduino.cc/en/Reference/Libraries、https://playground.arduino.cc/Code/库。\[3\] 综上所述,Arduino的键盘库可以帮助您读取矩阵类型的键盘,并且支持各种键盘布局和配置。您可以按照示例草图和文档的说明来使用和定制键盘库。 #### 引用[.reference_title] - *1* [arduinoKeypad 库函数介绍](https://blog.csdn.net/acktomas/article/details/117119504)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [arduinokeyboard库的使用](https://blog.csdn.net/acktomas/article/details/117118991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值