Arduino en español
Circuitos con Arduino - Juan Antonio Villalpando
-- Tutorial de iniciación a Arduino --
Volver al índice del tutorial
____________________________
49.- Teclado táctil.
- El terminal 1 es el de la izquierda del teclado como se indica en la imagen superior. |
|
- Este tipo de teclado se denomina en inglés keyboard o keypad.
- El problema de este tipo de teclado es que necesita muchos terminales de conexión, este concretamente necesita 8.
- El teclado está formado por una matriz de 4 columnas y 4 filas. 4 conectores son columnas y los otros 4 son filas.
- Conectamos el teclado directamente al Arduino siguiendo la disposición de la siguiente tabla:
Terminal del teclado |
Terminal del Arduino |
1 |
D9 |
2 |
D8 |
3 |
D7 |
4 |
D6 |
5 |
D5 |
6 |
D4 |
7 |
D3 |
8 |
D2 |
____________________________________
- Librería.
keyboard.zip
- En su carpeta examples hay varios ejemplos.
____________________________________
- Código.
- Pulsamos una tecla y aparecerá su carácter en el Serial Monitor
Código |
/*
Juan A. Villalpando
kio4.com
*/
#include <Keypad.h>
const byte numRows= 4; // Tiene 4 filas
const byte numCols= 4; // Tiene 4 columnas
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6}; // Estos terminales del Arduino corresponden a Filas
byte colPins[numCols]= {5,4,3,2}; // Estos terminales del Arduino corresponden a Columnas
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
}
// Escribe la tecla pulsada en el Serial Monitor.
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}
|
___________________________
- Podemos cambiar la salida del teclado, es decir, si ponemos...
char keymap[numRows][numCols]=
{
{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p'}
};
...saldrán esos caracteres cuando pulsemos las teclas correspondientes.
____________________________________
- Otro código para el teclado. Sin librería.
- Este otro código no necesita librería. Obtenido de...
http://www.instructables.com/id/Keypad-With-Arduino-Without-USing-Keypad-library-F/
- Las conexiones entre teclado y Arduino son distintas a las del código anterior, pero puedes hacer las pruebas dejándolas como en el código anterior.
Código |
/*this keypad tutorial number one by omar tarek 29-06-2014 */
byte h=0,v=0; //variables used in for loops
const unsigned long period=50; //little period used to prevent error
unsigned long kdelay=0; // variable used in non-blocking delay
const byte rows=4; //number of rows of keypad
const byte columns=4; //number of columnss of keypad
const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad
const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of keypad
byte keypad() // function used to detect which button is used
{
static bool no_press_flag=0; //static flag used to ensure no button is pressed
for(byte x=0;x<columns;x++) // for loop used to read all inputs of keypad to ensure no button is pressed
{
if (digitalRead(Input[x])==HIGH); //read evry input if high continue else break;
else
break;
if(x==(columns-1)) //if no button is pressed
{
no_press_flag=1;
h=0;
v=0;
}
}
if(no_press_flag==1) //if no button is pressed
{
for(byte r=0;r<rows;r++) //for loop used to make all output as low
digitalWrite(Output[r],LOW);
for(h=0;h<columns;h++) // for loop to check if one of inputs is low
{
if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue
continue;
else //if one of inputs is low
{
for (v=0;v<rows;v++) //for loop used to specify the number of row
{
digitalWrite(Output[v],HIGH); //make specified output as HIGH
if(digitalRead(Input[h])==HIGH) //if the input that selected from first sor loop is change to high
{
no_press_flag=0; //reset the no press flag;
for(byte w=0;w<rows;w++) // make all outputs as low
digitalWrite(Output[w],LOW);
return v*4+h; //return number of button
}
}
}
}
}
return 50;
}
void setup()
{
for(byte i=0;i<rows;i++) //for loop used to make pin mode of outputs as output
{
pinMode(Output[i],OUTPUT);
}
for(byte s=0;s<columns;s++) //for loop used to makk pin mode of inputs as inputpullup
{
pinMode(Input[s],INPUT_PULLUP);
}
Serial.begin(9600); //to use serial monitor we set the buad rate
}
void loop()
{
if(millis()-kdelay>period) //used to make non-blocking delay
{
kdelay=millis(); //capture time from millis function
switch (keypad()) //switch used to specify which button
{
case 0:
Serial.println(1);
break;
case 1:
Serial.println(2);
break;
case 2:
Serial.println(3);
break;
case 3:
Serial.println("F1");
break;
case 4:
Serial.println(4);
break;
case 5:
Serial.println(5);
break;
case 6:
Serial.println(6);
break;
case 7:
Serial.println("F2");
break;
case 8:
Serial.println(7);
break;
case 9:
Serial.println(8);
break;
case 10:
Serial.println(9);
break;
case 11:
Serial.println("F3");
break;
case 12:
Serial.println("Mode");
break;
case 13:
Serial.println(0);
break;
case 14:
Serial.println("Cancel");
break;
case 15:
Serial.println("Enter");
break;
default:
;
}
}
}
|
____________________________________
- Otro modelo de teclado.
- Este modelo es más sólido que el anterior, pero tiene un precio un poco mayor.
- La placa mide 4 x 4 cm. El modelo que vimos al principio mide 7 x 7 cm.
- Podemos utilizar los mismos códigos que hemos visto anteriormente.
- Necesita conectarse 8 pines del Arduino, por lo cual deja menos pines disponibles.
_________________
- Problema.
- El principal problema de este tipo teclado es que ocupa 8 terminales del Arduino.
- Teclado I2C.
- La gran ventaja de los teclado con bus I2C es que solo necesitan 2 conexiones de terminales, además de la alimentación, en total 4.
- La desventaja es el precio, viene a costar unos 10 €.
- La conexión es mediante el Bus I2C, sus terminales son SCL y SDA, como hemos visto en otros tutoriales de I2C.
https://github.com/joeyoung/arduino_keypads
- Adaptación de un teclado de matriz 4 x 4 a un teclado I2C.
- Esto lo veremos en el siguiente tutorial: 49B_teclado_I2C.htm
________________________________
|