📜  arduino taster (1)

📅  最后修改于: 2023-12-03 15:29:27.631000             🧑  作者: Mango

Arduino Taster

Arduino Taster is a library that simplifies the use of tactile switches (also known as buttons) on Arduino boards. This library provides a set of functions to detect button presses, releases, and long presses. With this library, you can easily add a user interface to your projects.

Installation

To use the Arduino Taster library, you first need to install it. Here's how:

  1. Download the latest version of the library from GitHub.

  2. Extract the ZIP file to your Arduino libraries folder. By default, this folder is located in the Arduino application folder on your computer.

    Arduino/libraries/
    
  3. Restart the Arduino IDE.

Usage

Using the Arduino Taster library is easy. Here's an example code snippet that demonstrates how to use it:

#include <Taster.h>

// Define the button pin
const int BUTTON_PIN = 2;

// Create a new Taster object
Taster myButton(BUTTON_PIN);

void setup() {
  // Initialize Serial communication
  Serial.begin(9600);

  // Set the button mode
  myButton.setMode(Taster::PULLUP);
}

void loop() {
  // Check if the button is pressed
  if (myButton.isPressed()) {
    Serial.println("Button pressed");
  }

  // Check if the button is released
  if (myButton.isReleased()) {
    Serial.println("Button released");
  }

  // Check if the button is being held down (long press)
  if (myButton.isLongPressed()) {
    Serial.println("Button long press");
  }
}

In this example, we create a Taster object and set it to use the internal pull-up resistor (which is the most common setup for tactile switches). We then check for button presses, releases, and long presses in the loop() function and output a message to the Serial Monitor whenever one of these events occurs.

API

The Arduino Taster library provides the following functions:

Taster(uint8_t pin)

The class constructor. Takes a pin number as an argument.

void setMode(Taster::Mode mode)

Sets the button mode. mode can be one of the following values:

  • Taster::PULLUP (default)
  • Taster::PULLDOWN
void setDebounceTime(uint16_t debounceTime)

Sets the debounce time in milliseconds. Default is 50 ms.

bool isPressed()

Returns true if the button is currently pressed, otherwise false.

bool isReleased()

Returns true if the button was just released, otherwise false.

bool isLongPressed()

Returns true if the button is being held down for a long time, otherwise false.

Conclusion

The Arduino Taster library provides a simple and efficient way to work with tactile switches on Arduino boards. With just a few lines of code, you can add user input to your projects. If you have any questions or feedback, don't hesitate to leave a comment.