ezButton-按钮库

ezButton-按钮库

使用按钮时,初学者通常会遇到以下麻烦:

  • Floating input issue 浮动输入问题
  • Chattering issue 抖动问题
  • Detecting the pressed and released events
    检测按下和释放的事件
  • Managing timestamp when debouncing for multiple buttons
    在多个按钮去抖动时管理时间戳

The ezButton (easy button) library is designed to solve all of the above problems and make it easy to use for not only beginners but also experienced users. It is created by ArduioGetStarted.com.
ezButton(简易按钮)库旨在解决上述所有问题,不仅对初学者而且对有经验的用户都易于使用。它是由 ArduioGetStarted.com 创建的。

The library can be used for push button, momentary switches, toggle switch, magnetic contact switch (door sensor)…
该库可用于按钮、瞬时开关、拨动开关、磁接触开关(门传感器)…

Arduino button library feature

Library Features 库功能

  • Uses the internal pull-up resistor to avoid the floating value
    使用内部上拉电阻器来避免浮点值
  • Supports debounce to eliminate the chattering phenomenon
    支持去抖动,消除抖动现象
  • Supports the pressed and released events
    支持按下和释放事件
  • Supports the counting (for FALLING, RISING and BOTH)
    支持计数(用于 FALLING、RISING 和 BOTH)
  • Easy to use with multiple buttons
    易于使用,带有多个按钮
  • All functions are non-blocking
    所有功能都是非阻塞的

※ NOTE THAT: ※ 注意事项:

How To Install Library 如何安装库

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
    导航到 Arduino IDE 左侧栏上的 Libraries 图标。

Search “ezButton”, then find the button library by ArduinoGetStarted
搜索“ezButton”,然后通过ArduinoGetStarted找到按钮库

Click Install button to install ezButton library.
单击“安装”按钮以安装 ezButton 库。

Arduino button library

Or you can download it on Github
或者您可以在 Github 上下载它Ezoic

EzoicExamples 例子

  • Example - Single Button
    示例 - 单按钮
  • Example - Single Button Events
    示例 - 单按钮事件
  • Example - Single Button Debounce
    示例 - 单按钮去抖
  • Example - Single Button All
    示例 - 全部单按钮
  • Example - Multiple Button All
    示例 - 多按钮
  • Example - Button Count
    示例 - 按钮计数
  • Example - Button Array
    示例 - 按钮数组

EzoicFunctions 功能

  • `ezButton()``
  • setDebounceTime()
  • getState()
  • getStateRaw()
  • isPressed()
  • isReleased()
  • setCountMode()
  • getCount()
  • resetCount()
  • loop()

References 参考

EzoicezButton()

Description 描述

This function creates a new instance of the ezButton class that represents a particular button attached to your Arduino board.
此函数创建 ezButton 类的新实例,该类表示连接到 Arduino 板的特定按钮。

Syntax 语法

ezButton(pin)

Parameters 参数

pin: int - Arduino’s pin that connects to the button.
pin: int - 连接到按钮的 Arduino 引脚。

Return 返回

A new instance of the ezButton class.
ezButton 类的新实例。

Example 例

ezButton button(7);

setDebounceTime()

Description 描述

This function is used to set the debounce time. The debounce time takes effect on getState(), isPressed(), isReleased() and getCount().
此函数用于设置去抖动时间。去抖动时间对 getState()isPressed()isReleased()getCount() 生效。

Syntax 语法

button.setDebounceTime(time);

Parameters 参数

time: unsigned long - debounce time in milliseconds
时间:无符号长整数 - 去抖动时间(以毫秒为单位)

Return 返回

None 没有

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  button.setDebounceTime(100); // set debounce time to 100 milliseconds
}

void loop() {}

getState()

Description 描述

This function returns the state of the button. If the debounce time is set, the returned state is the state after debouncing. We MUST call button.loop() function before using this function.
此函数返回按钮的状态。如果设置了去抖动时间,则返回的状态是去抖动后的状态。在使用此函数之前,我们必须调用 button.loop() 函数。

Syntax 语法

button.getState();

Parameters 参数

None 没有

Return 返回

The state of button. Since the ezButton library used the internal pull-up resistor, the state will be HIGH (1) when no press, and LOW (0) when pressed. If the debounce time is set, the returned state is the state after debouncing.
按钮的状态。由于ezButton库使用内部上拉电阻,因此不按下时状态为高电平(1),按下时状态为低电平(0)。如果设置了去抖动时间,则返回的状态是去抖动后的状态。

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  
}

void loop() {
  button.loop(); // MUST call the loop() function first

  int btnState = button.getState();
}



getStateRaw()

This function returns the current state of the button without debouncing. It is equivalent to digitalRead().
此函数返回按钮的当前状态,而不进行去抖动。它等同于 digitalRead()

Description 描述
Syntax 语法

button.getStateRaw()

Parameters 参数

None 没有

Return 返回

The current state of button without debouncing.
按钮的当前状态,没有去抖动。

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  
}

void loop() {
  int btnState = button.getStateRaw();
}

isPressed()

Description 描述

This function check whether a button is pressed or not. We MUST call button.loop() function before using this function.
此功能检查按钮是否被按下。在使用此函数之前,我们必须调用 button.loop() 函数。

Syntax 语法

button.isPressed()

Parameters 参数

None 没有

Return 返回

true if the button is pressed, false otherwise
如果按下按钮,则为 true,否则为 false

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

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

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed())
    Serial.println("The button is pressed");
}

isReleased()

Description 描述

This function check whether a button is released or not. We MUST call button.loop() function before using this function.
此功能检查按钮是否被释放。在使用此函数之前,我们必须调用 button.loop() 函数。

Syntax 语法

button.isReleased()

Parameters 参数

None 没有

Return 返回

true if the button is released, false otherwise
如果释放按钮,则为 true,否则为 false

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

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

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isReleased())
    Serial.println("The button is released");
}

setCountMode()

Description 描述

This function sets the count mode.
此函数设置计数模式。

Syntax 语法

button.setCountMode(mode)

Parameters 参数

mode: int - count mode. The available count modes include: COUNT_FALLING, COUNT_RISING and COUNT_BOTH.
mode: int - 计数模式。可用的计数模式包括:COUNT_FALLINGCOUNT_RISINGCOUNT_BOTH

Return 返回

None 没有

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(100); // set debounce time to 100 milliseconds
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();
  Serial.println(count);
}



getCount()

Description 描述

This function returns the count value. We MUST call button.loop() function before using this function.
此函数返回计数值。在使用此函数之前,我们必须调用 button.loop() 函数。

Syntax 语法

button.getCount()

Parameters 参数

None. 没有。

Return 返回

The count value. If the debounce time is set, the returned value is the value after debouncing.
计数值。如果设置了去抖动时间,则返回的值为去抖动后的值。

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(100); // set debounce time to 100 milliseconds
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();
  Serial.println(count);
}

resetCount()

Description 描述

This function set the counter value to 0.
此函数将计数器值设置为 0。

Syntax 语法

button.resetCount()

Parameters 参数

None. 没有。

Return 返回

None. 没有。

Example 例
#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();
  if(count >= 100)
    button.resetCount();
}

loop() 循环()

loop() 循环()

Description 描述

This function does debounce and updates the state of the button. It MUST be called before using getState(), isPressed(), isReleased() and getCount() functions.
此函数会去抖动并更新按钮的状态。在使用 getState()、isPressed()、isReleased() 和 getCount() 函数之前,必须调用它。

Syntax 语法

button.loop(); button.loop();

Parameters 参数

None 没有

Return 返回

None 没有

## 单个按钮Example

Wiring Diagram

在这里插入图片描述

#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed())
    Serial.println("The button is pressed");

  if(button.isReleased())
    Serial.println("The button is released");
}

#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(100); // set debounce time to 100 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  int btnState = button.getState();
  Serial.println(btnState);

  if(button.isPressed())
    Serial.println("The button is pressed");

  if(button.isReleased())
    Serial.println("The button is released");
}


按钮数组

Wiring Diagram

在这里插入图片描述

/*

 * Created by ArduinoGetStarted.com
   *
 * This example code is in the public domain
   *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
   *
 * This example shows how to use array of button.
   */

#include <ezButton.h>

const int BUTTON_NUM = 5;

const int BUTTON_1_PIN = 2;
const int BUTTON_2_PIN = 3;
const int BUTTON_3_PIN = 4;
const int BUTTON_4_PIN = 5;
const int BUTTON_5_PIN = 6;

ezButton buttonArray[] = {
  ezButton(BUTTON_1_PIN),
  ezButton(BUTTON_2_PIN),
  ezButton(BUTTON_3_PIN),
  ezButton(BUTTON_4_PIN),
  ezButton(BUTTON_5_PIN)
};

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

  for (byte i = 0; i < BUTTON_NUM; i++) {
    buttonArray[i].setDebounceTime(50); // set debounce time to 50 milliseconds
  }
}

void loop() {
  for (byte i = 0; i < BUTTON_NUM; i++)
    buttonArray[i].loop(); // MUST call the loop() function first

  for (byte i = 0; i < BUTTON_NUM; i++) {
    if (buttonArray[i].isPressed()) {
      Serial.print("The button ");
      Serial.print(i + 1);
      Serial.println(" is pressed");
    }

    if (buttonArray[i].isReleased()) {
      Serial.print("The button ");
      Serial.print(i + 1);
      Serial.println(" is released");
    }
  }
}

多个按钮

Wiring Diagram

在这里插入图片描述

This image is created using Fritzing. Click to enlarge image

Arduino Code

Quick Steps

  • Install ezButton library. See How To
  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • On Arduino IDE, Go to File Examples ezButton 05.MultipleButtonAll example
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
 *
 * This example:
 *   + uses debounce for multiple buttons.
 *   + reads state of multiple buttons
 *   + detects the pressed and released events of multiple buttons
 */

#include <ezButton.h>

ezButton button1(6);  // create ezButton object that attach to pin 6;
ezButton button2(7);  // create ezButton object that attach to pin 7;
ezButton button3(8);  // create ezButton object that attach to pin 8;

void setup() {
  Serial.begin(9600);
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  button3.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  button3.loop(); // MUST call the loop() function first

  int btn1State = button1.getState();
  int btn2State = button2.getState();
  int btn3State = button3.getState();
  Serial.print("button 1 state: ");
  Serial.println(btn1State);
  Serial.print("button 2 state: ");
  Serial.println(btn2State);
  Serial.print("button 3 state: ");
  Serial.println(btn3State);

  if(button1.isPressed())
    Serial.println("The button 1 is pressed");

  if(button1.isReleased())
    Serial.println("The button 1 is released");

  if(button2.isPressed())
    Serial.println("The button 2 is pressed");

  if(button2.isReleased())
    Serial.println("The button 2 is released");

  if(button3.isPressed())
    Serial.println("The button 3 is pressed");

  if(button3.isReleased())
    Serial.println("The button 3 is released");
}

  • Click Upload button on Arduino IDE to upload code to Arduino

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/717519.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu 20.04 LTS WslRegisterDistribution failed with error: 0x800701bc

1.以管理员身份运行powershell&#xff0c;输入&#xff1a;wsl --update&#xff0c; 2.重新打开ubuntu即可。

2024年春季学期《算法分析与设计》练习15

问题 A: 简单递归求和 题目描述 使用递归编写一个程序求如下表达式前n项的计算结果&#xff1a; (n<100) 1 - 3 5 - 7 9 - 11 ...... 输入n&#xff0c;输出表达式的计算结果。 输入 多组输入&#xff0c;每组输入一个n&#xff0c;n<100。 输出 输出表达式的计…

Linux时间子系统6:NTP原理和Linux NTP校时机制

一、前言 上篇介绍了时间同步的基本概念和常见的时间同步协议NTP、PTP&#xff0c;本篇将详细介绍NTP的原理以及NTP在Linux上如何实现校时。 二、NTP原理介绍 1. 什么是NTP 网络时间协议&#xff08;英语&#xff1a;Network Time Protocol&#xff0c;缩写&#xff1a;NTP&a…

解决 uniapp h5 页面在私有企微iOS平台 间歇性调用uni api不成功问题(uni.previewImage为例)。

demo <template><view class"content"><image class"logo" src"/static/logo.png"></image><button click"previewImage">预览图片</button></view> </template><script> //打…

WebGIS如何加载微件

本篇文章以加载切换底图微件做示范 首先&#xff0c;添加require "esri/widgets/ScaleBar",//比例尺"esri/widgets/Legend",//图例"esri/widgets/basemapGallery" 然后添加加载切换底图的组件代码 const basemapGallery new BasemapGallery(…

如何下载mmwave_automotive_toolbox?

摘要&#xff1a;mmwave_automotive_toolbox已经没有下载连接了&#xff0c;因为它已经和radar_toolbox集成到一起了&#xff0c;本文介绍下载方法。 链接如下 Corner Radar Overview (ti.com) 本文发布的时间时2024年6月17日&#xff0c;如果上面这个链接已经无法访问&#…

3D Gaussian Splatting Windows安装

1.下载源码 git clone https://github.com/graphdeco-inria/gaussian-splatting --recursive 2.安装cuda NVIDIA GPU Computing Toolkit CUDA Toolkit Archive | NVIDIA Developer 3.安装COLMAP https://github.com/colmap/colmap/releases/tag/3.9.1 下载完成需要添加环…

AI产品经理,应掌握哪些技术?

美国的麻省理工学院&#xff08;Massachusetts Institute of Technology&#xff09;专门负责科技成果转化商用的部门研究表明&#xff1a; 每一块钱的科研投入&#xff0c;需要100块钱与之配套的投资&#xff08;人、财、物&#xff09;&#xff0c;才能把思想转化为产品&…

Stable Diffusion文生图模型训练入门实战(完整代码)

Stable Diffusion 1.5&#xff08;SD1.5&#xff09;是由Stability AI在2022年8月22日开源的文生图模型&#xff0c;是SD最经典也是社区最活跃的模型之一。 以SD1.5作为预训练模型&#xff0c;在火影忍者数据集上微调一个火影风格的文生图模型&#xff08;非Lora方式&#xff…

记录一次基于Vite搭建Vue3项目的过程

Vue2已经于2023年12月31日停止维护了&#xff0c;2024年算是vue3的崭新的一年&#xff0c;我们的项目也基本从vue2逐渐向着Vue3过渡&#xff0c;Vue3相较于vue2有更好的开发体验&#xff0c;和ts的自然融合使得项目的结构、功能拆分变得更加的清晰&#xff1b;组合式声明有种MV…

vulnhub靶机hacksudoLPE中Challenge-2

二、Challenge-2 1. ar Abusing 这个是要利用suid注意sudo也可以用&#xff0c;但是还是按照要求来 注意使用的suid自然是home文件夹 2. ash abusing 33. atobm Abusing 环境有问题&#xff0c;做不了 34. base32 Abusing 35. bash Abusing 36. cat Abusing 37. chmod Abusin…

视角概述( Perspective 业务分析篇)

背景 在业务分析工作中使用透视图来提供对特定于计划上下文的任务和技术的关注。大多数提案可能涉及一个或多个视角。视角主要包括&#xff1a; •敏捷•商业智能•信息技术•商业架构&#xff0c;以及业务流程管理。这些视角并不代表业务分析实践的所有可能视角。 任何给定…

HTTP/2 协议学习

HTTP/2 协议介绍 ​ HTTP/2 &#xff08;原名HTTP/2.0&#xff09;即超文本传输协议 2.0&#xff0c;是下一代HTTP协议。是由互联网工程任务组&#xff08;IETF&#xff09;的Hypertext Transfer Protocol Bis (httpbis)工作小组进行开发。是自1999年http1.1发布后的首个更新。…

kotlin类型检测与类型转换

一、is与!is操作符 1、使用 is 操作符或其否定形式 !is 在运行时检测对象是否符合给定类型。 fun main() {var a "1"if(a is String) {println("a是字符串类型:${a.length}")}// 或val b a is Stringprintln(b) } 二、"不安全的"转换操作符…

直播无线麦克风哪个好?一文揭秘无线领夹麦克风哪个牌子好!

​在人人可做自媒体的时代&#xff0c;众多普通人加入自媒体。对拍视频的自媒体人&#xff0c;好内容是基础&#xff0c;好设备是保障。想提升视频音质需专业无线麦克风。现无线麦克风品牌多&#xff0c;如何少花钱买高性价比产品是问题。作为资深自媒体人&#xff0c;我用过的…

基于振弦采集仪的地下综合管廊工程安全监测技术研究

基于振弦采集仪的地下综合管廊工程安全监测技术研究 地下综合管廊工程是一项重要的城市基础设施工程&#xff0c;承载着城市供水、供电、供热、排水等重要功能。为了确保地下综合管廊工程的安全运行&#xff0c;需要进行有效的安全监测。本文将重点研究基于振弦采集仪的地下综…

【YOLOv10改进[注意力]】在YOLOv10中使用注意力ECA(2020.4)的实践+ 含全部代码和详细修改方式 + 手撕结构图 + 全网首发

本文将进行在YOLOv10中添加注意力ECA的实践,助力YOLOv10目标检测效果的实践,文中含全部代码、详细修改方式以及手撕结构图。助您轻松理解改进的方法。 改进前和改进后的参数对比: 目录 一 ECA 二 在YOLOv10中使用注意力ECA的实践 1 整体修改

为什么idea总是提示将内部类设置为static

在写一些内部类的时候&#xff0c;Idea总是提示要设置为static&#xff0c;你知道为什么吗 在Java中&#xff0c;内部类可以被声明为static&#xff0c;这种内部类称为静态内部类&#xff08;Static Nested Class&#xff09;。静态内部类和非静态内部类有显著的区别&#xf…

PLSQL、Oracle以及客户端远程连接服务器笔记(仅供参考)

1.PLSQL参考链接&#xff1a; 全网最全最细的PLSQL下载、安装、配置、使用指南、问题解答&#xff0c;相关问题已汇总-CSDN博客文章浏览阅读2.9w次&#xff0c;点赞98次&#xff0c;收藏447次。双击之后&#xff0c;这里选择安装目录&#xff0c;你安装目录选的哪里&#xff0…

SSM小区疫情防控系统-计算机毕业设计源码03748

摘 要 随着社会的发展&#xff0c;社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 小区疫情防控系统&#xff0c;主要的模块包括查看首页、轮播图&#xff08;轮播图管理&#xff09;、社区公告管理&#xff08;社区公告&#…