Nota

Ciao, benvenuto nella Community di SunFounder per appassionati di Raspberry Pi, Arduino e ESP32 su Facebook! Approfondisci Raspberry Pi, Arduino ed ESP32 insieme ad altri appassionati.

Perché unirsi?

  • Supporto Esperto: Risolvi problemi post-vendita e sfide tecniche con l’aiuto della nostra comunità e del nostro team.

  • Impara e Condividi: Scambia suggerimenti e tutorial per migliorare le tue competenze.

  • Anteprime Esclusive: Accedi in anteprima agli annunci dei nuovi prodotti e alle anticipazioni.

  • Sconti Speciali: Godi di sconti esclusivi sui nostri prodotti più recenti.

  • Promozioni e Giveaway Festivi: Partecipa a giveaway e promozioni festive.

👉 Pronto a esplorare e creare con noi? Clicca su [Qui] e unisciti oggi!

Tastierino

Panoramica

In questa lezione, imparerai a usare un tastierino. Il tastierino può essere applicato a vari tipi di dispositivi, tra cui telefoni cellulari, fax, forni a microonde e così via. È comunemente usato per l’input dell’utente.

Componenti Necessari

In questo progetto, abbiamo bisogno dei seguenti componenti.

È sicuramente conveniente acquistare un kit completo, ecco il link:

Nome

ELEMENTI IN QUESTO KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

Puoi anche acquistarli separatamente dai link seguenti.

INTRODUZIONE DEI COMPONENTI

LINK PER L’ACQUISTO

Arduino Uno R4 WiFi

-

Cavi Jumper

ACQUISTA

Tastierino

ACQUISTA

Cablaggio

../_images/21-keypad_bb.png

Schema Elettrico

../_images/21_keypad_schematic.png

Codice

Nota

  • Puoi aprire direttamente il file 21-keypad.ino nel percorso elite-explorer-kit-main\basic_project\21-keypad.

  • Per installare la libreria, usa il Gestore Librerie di Arduino e cerca «Adafruit Keypad» e installala.

21-keypad.ino
 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}

Dopo aver caricato il codice sulla scheda UNO, sul monitor seriale, puoi vedere il valore del tasto attualmente premuto sul tastierino.

Analisi del Codice

  1. Inclusione della Libreria

    Iniziamo includendo la libreria Adafruit_Keypad, che ci permette di interfacciarci facilmente con il tastierino.

    #include "Adafruit_Keypad.h"
    

    Nota

    • Per installare la libreria, usa il Gestore Librerie di Arduino e cerca «Adafruit Keypad» e installala.

  2. Configurazione del Tastierino

    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 };
    
    • Le costanti ROWS e COLS definiscono le dimensioni del tastierino.

    • keys è un array 2D che memorizza l’etichetta di ciascun pulsante del tastierino.

    • rowPins e colPins sono array che memorizzano i pin Arduino collegati alle righe e colonne del tastierino.


  3. Inizializzazione del Tastierino

    Crea un’istanza di Adafruit_Keypad chiamata myKeypad e inizializzala.

    Adafruit_Keypad myKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
    
  4. Funzione setup()

    Inizializza la comunicazione Serial e il tastierino personalizzato.

    void setup() {
      Serial.begin(9600);
      myKeypad.begin();
    }
    
  5. Ciclo Principale

    Controlla gli eventi dei tasti e visualizzali nel Monitor Seriale.

    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);
    }