arduino中Keypad 库函数介绍

arduino中Keypad 库函数介绍原文:https://playground.arduino.cc/Code/Keypad/Creation构造函数:Keypad(makeKeymap(userKeymap),row[],col[],rows,cols)constbyterows=4;//fourrowsconstbytecols=3;//threecolumnscharkeys[rows][cols]={{‘1′,’2′,’3’},{‘4′,’5′,’6’},{‘

大家好,又见面了,我是你们的朋友全栈君。

原文: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毫秒按一次键。如果在该时间范围内发生了多次按键操作(如按键弹起时会发生这种情况),那么这些多余的按键操作将被忽略。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134213.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • egg yolk_人类蛋白数据库

    egg yolk_人类蛋白数据库欢迎关注”生信修炼手册”!直系同源蛋白的预测在系统发育,比较基因组学等多个领域都占用重要地位,COG数据库开创了同源蛋白数据库的先河,后续又不断有新的数据库涌现,而eggNOG就是目前使…

    2022年10月29日
  • 555施密特触发器电路图_555定时器构成的施密特触发器

    555施密特触发器电路图_555定时器构成的施密特触发器目录方法作用内部电路分析555内部电路图分析仿真电路图仿真结果总结将555的6脚和2脚连接在一起,并在5脚接上0.01uF的电容用于滤波,这就构成了施密特触发器。施密特触发器可作为波形整形电路,能将模拟信号波形整形为数字电路能够处理的方波波形,而且由于施密特触发器具有滞回特性,所以可用于抗干扰,其应用包括在开回路配置中用于抗扰,以及在闭回路正回授/负回授配置中用于实现多谐振荡器。百度百科:https://baike.baidu.com/item/%E6%96%

    2022年10月26日
  • 3.7v锂电池升压电路_电池升压

    3.7v锂电池升压电路_电池升压FS2114的PCB布局设计建议-基础篇开关电源的一个常见问题是“不稳定”的开关波形。有时,波形抖动很明显,可以听到从磁性元件发出噪声。如果问题与印刷电路板(PCB)布局有关,则很难确定原因。EMC也是很注重(PCB)布局,这就是为什么在开关电源设计的早期正确布局PCB至关重要的原因。其重要性不可夸大。原理图走线主要器件放置并联一个旁路电容0.1uFLX节点FB反馈电阻R1,R2COUT电容容易影响输出的布线功率组件的推荐焊盘图案GND功率地的PCB布线电感器选择概述FS2

    2022年10月7日
  • 浅谈SQL游标

    浅谈SQL游标
    游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。游标允许你选择一组数据,通过翻阅这组数据记录——通常被称为数据集,检查每一个游标所在的特定的行。你可以将游标和局部变量组合在一起对每一个记录进行检查,当游标移动到下一个记录时,来执行一些外部操作。游标的另一个常见的用法是:保存查询结果以备以后使用。一个游标结果集是通过执

    2022年7月12日
  • Ubuntu安装GCC编译器

    Ubuntu安装GCC编译器没有gcc啊如何安装gccapt-getinstallbuild-essential

    2022年7月24日
  • JS中indexOf的用法 「建议收藏」

    JS中indexOf的用法 「建议收藏」String.IndexOf(Char,[startIndex],[count]):返回指定字符在原字符串中的第一个匹配项的索引。可指定字符开始检索位置和指定长度的字符,若没有找到该字符,则返回

    2022年7月4日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号