Skip to content

Example 02: Digital signal readings

Alexander Spiridonov edited this page Mar 19, 2024 · 12 revisions

Additional documentation for an example supplied with the KeyDetector library that demonstrates use of a library to get digital signal readings.

About

This example shows how KeyDetector can be used to detect single, continuous and simultaneous push-button presses. Pressing button once or keeping it in a pressed state continuously will print corresponding message. Pressing another button in combination with the one already in a pressed state will be detected as well (since KeyDetector version 1.2.0).

Parts List

  • Arduino UNO or compatible
  • Three momentary switches
  • Three 10kOhm resistors

Schematic

Schematic is simple. Each switch is connected to the input pin of Arduino using pulldown resistor. You may wish to implement additional debounce filtering to minify chances of false readings at the moment of button press (or try to increase value of the debounceDelay parameter during library initialization).

Breadboard

Connect one terminal of each momentary switch both to its corresponding Arduino input pin (2, 3, or 4) and to ground through 10kOhm pulldown resistor. Connect the opposite terminal to 5V.

Sketch

Annotated sketch is supplied with the library and can be found at "examples/Example-02_Digital/Example-02_Digital.ino".

Simulator

Simulation of this example is available on Wokwi. It is possible to interact with the simulation using mouse or keyboard: keys 1, 2, 3 are bound to the corresponding buttons on the breadboard (make sure to focus simulation window by clicking on it first).

Run

After compiling and uploading sketch to Arduino, open Serial Monitor and start pressing the buttons. Message Button X pressed! (where X is the name of the button, being A, B, or C) will be printed for each button press. If you press and hold the button in a pressed state for some time, after about 1s message Button X remains pressed will be printed, and it will be continuously repeated with 0.5s interval while button remains in a pressed state. Pressing another button in combination with the one already in a pressed state will result in a message Button Y pressed simultaneously with Button X! (where X is the name of the button already in a pressed state, and Y - button currently pressed) being printed.

Clone this wiki locally