注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!
キーパッド
概要
このレッスンでは、キーパッドの使用方法を学びます。キーパッドは、携帯電話、ファックス機、電子レンジなど、さまざまな種類のデバイスに適用できます。一般的にユーザー入力に使用されます。
必要なコンポーネント
このプロジェクトには、以下のコンポーネントが必要です。
全セットを購入するのが便利です。こちらがリンクです:
名称 |
このキットのアイテム数 |
リンク |
|---|---|---|
Elite Explorer Kit |
300+ |
以下のリンクから個別に購入することもできます。
コンポーネント紹介 |
購入リンク |
|---|---|
- |
|
配線図
回路図
コード
注釈
ファイル
21-keypad.inoをelite-explorer-kit-main\basic_project\21-keypadのパスで直接開くことができます。ライブラリをインストールするには、Arduinoライブラリマネージャーを使用し、 「Adafruit Keypad」 を検索してインストールしてください。
1/*
2 The code use an Adafruit Keypad and display keypress events via the Serial Monitor.
3 It initializes the keypad with a 4x4 layout, where each key is represented by a
4 specific character. It continuously checks for keypad events (key press or release)
5 and prints the corresponding key and event status to the Serial Monitor.
6
7 Board: Arduino Uno R4
8 Component: Keypad
9 Library: https://github.com/adafruit/Adafruit_Keypad (Adafruit Keypad by Adafruit)
10*/
11
12// Include the Adafruit_Keypad library
13#include "Adafruit_Keypad.h"
14
15// Define the number of rows and columns for the keypad
16const byte ROWS = 4;
17const byte COLS = 4;
18
19// Define the characters mapped to each button on the 4x4 keypad
20char keys[ROWS][COLS] = {
21 { '1', '2', '3', 'A' },
22 { '4', '5', '6', 'B' },
23 { '7', '8', '9', 'C' },
24 { '*', '0', '#', 'D' }
25};
26
27// Define the Arduino pins connected to the row pinouts of the keypad
28byte rowPins[ROWS] = { 2, 3, 4, 5 };
29// Define the Arduino pins connected to the column pinouts of the keypad
30byte colPins[COLS] = { 8, 9, 10, 11 };
31
32// Initialize a custom keypad instance
33Adafruit_Keypad myKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
34
35// Setup function
36void setup() {
37 // Initialize Serial communication at 9600 baud rate
38 Serial.begin(9600);
39 // Initialize the custom keypad
40 myKeypad.begin();
41}
42
43// Main loop function
44void loop() {
45 // Update the state of keys
46 myKeypad.tick();
47
48 // Check if there are new keypad events
49 while (myKeypad.available()) {
50 // Read the keypad event
51 keypadEvent e = myKeypad.read();
52 // Print the key that triggered the event
53 Serial.print((char)e.bit.KEY);
54 // Print the type of event: pressed or released
55 if (e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed");
56 else if (e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released");
57 }
58
59 delay(10);
60}
UNOボードにコードをアップロードした後、シリアルモニターで、キーパッドで現在押されているキーの値を確認することができます。
コード解析
ライブラリのインクルード
まず、キーパッドと簡単にインターフェースするための
Adafruit_Keypadライブラリをインクルードします。#include "Adafruit_Keypad.h"
注釈
ライブラリをインストールするには、Arduinoライブラリマネージャーを使用し、 「Adafruit Keypad」 を検索してインストールしてください。
キーパッドの設定
const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, { '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } }; byte rowPins[ROWS] = { 2, 3, 4, 5 }; byte colPins[COLS] = { 8, 9, 10, 11 };
ROWSおよびCOLSの定数はキーパッドの寸法を定義します。keysは2次元配列で、キーパッドの各ボタンのラベルを格納しています。rowPinsとcolPinsは、キーパッドの行と列に接続されているArduinoピンを格納する配列です。
キーパッドの初期化
Adafruit_KeypadのインスタンスをmyKeypadとして作成し、初期化します。Adafruit_Keypad myKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
setup()関数
シリアル通信とカスタムキーパッドを初期化します。
void setup() { Serial.begin(9600); myKeypad.begin(); }
メインループ
キーイベントをチェックし、シリアルモニターに表示します。
void loop() { myKeypad.tick(); while (myKeypad.available()) { keypadEvent e = myKeypad.read(); Serial.print((char)e.bit.KEY); if (e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed"); else if (e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released"); } delay(10); }