Vídeo anterior, com mais detalhes sobre a montagem do teclado:
http://fabianoallex.blogspot.com.br/2015/05/arduino-texto-com-teclado-matricial-4x3.html
Nessa versão implementei a função de backspace e mostrei o resultado num display LCD.
código-fonte:
#define col1 10
#define col2 11
#define col3 12
#define lin1 3
#define lin2 4
#define lin3 5
#define lin4 6
char get_tecla(){
int l[]={lin1, lin2, lin3, lin4}; // Array de 4 posições contendo os 4 pinos de linhas
int i = 0, k = 0, t = 0;
for (i=0; i<4; i++) {
digitalWrite(lin1,LOW);
digitalWrite(lin2,LOW);
digitalWrite(lin3,LOW);
digitalWrite(lin4,LOW);
digitalWrite(l[i],HIGH);
if(digitalRead(col1)) { t = i*3+1; break; }
if(digitalRead(col2)) { t = i*3+2; break; }
if(digitalRead(col3)) { t = i*3+3; break; }
}
if (t > 0 ){
if (t >= 1 && t<=9){ return char(t+48); } //48--> ASCII: o charactere '1' na tabela ascii é 49º item, o '2' é o 50º item e assim por diante
if (t==10) { return '*'; }
if (t==11) { return '0'; }
if (t==12) { return '#'; }
}
return '\0';
}
const char teclas[][12] = {"1 ", "2abc", "3def", "4ghi", "5jkl", "6mno", "7pqrs", "8tuv", "9wxyz", "*", "0", "#"};
const unsigned long time_char = 1200; //1200 milissegundos pra desconsiderar a ultima tecla
class TecladoTelefone{
private:
unsigned long _millis_last_char;
char _last_char;
String _palavra;
void _set_last_char(char c, int ind_palavra){
if ( ind_palavra == 1 && _last_char != '\0' ) {
_palavra += _last_char;
}
_last_char = c;
_millis_last_char = millis();
}
void _back_space(){
if (_palavra.length() >= 1){
_last_char = _palavra[_palavra.length()-1];
_palavra = _palavra.substring(0, _palavra.length()-1);
} else {
_last_char = '\0';
}
}
public:
TecladoTelefone(){
_millis_last_char = millis();
_last_char = '\0';
}
int is_timing(){
return ( (millis() - time_char) < _millis_last_char );
}
void add(char c){
int pos = -1;
if (c == '1'){ pos = 0;}
if (c == '2'){ pos = 1;}
if (c == '3'){ pos = 2;}
if (c == '4'){ pos = 3;}
if (c == '5'){ pos = 4;}
if (c == '6'){ pos = 5;}
if (c == '7'){ pos = 6;}
if (c == '8'){ pos = 7;}
if (c == '9'){ pos = 8;}
if (c == '*'){ pos = 9;}
if (c == '0'){ pos = 10;}
if (c == '#'){ pos = 11;}
if (pos == -1){ return; }
if (pos == 9){
_back_space();
return;
}
const char * t = teclas[pos];
if ( is_timing() ) {
int i = 0;
while (t[i] != '\0'){
if (_last_char == t[i]){
_set_last_char( (t[i+1] == '\0') ? _last_char = t[0] : _last_char = t[i+1] , 0 );
return ;
}
i++;
}
}
_set_last_char ( c, 1);
}
char get_last_char(){ return _last_char; }
String get_palavra( ) {
if (_last_char != '\0') {
return _palavra + _last_char;
}
return _palavra;
}
};
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 13, 2, 1, 0); //Configura os pinos do Arduino para se comunicar com o LCD
TecladoTelefone * teclado;
void setup() {
//Serial.begin(9600);
// colunas INPUT
pinMode(col1,INPUT);
pinMode(col2,INPUT);
pinMode(col3,INPUT);
// linhas OUTPUT
pinMode(lin1,OUTPUT);
pinMode(lin2,OUTPUT);
pinMode(lin3,OUTPUT);
pinMode(lin4,OUTPUT);
teclado = new TecladoTelefone();
lcd.begin(16, 2);
}
char tecla_anterior = '\0';
void loop() {
char tecla = get_tecla();
if (tecla != tecla_anterior){
if (tecla) {
teclado->add(tecla);
}
}
tecla_anterior = tecla;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(teclado->get_palavra());
lcd.cursor();
lcd.setCursor(teclado->get_palavra().length() - (teclado->is_timing() ? 1 : 0 ), 0);
delay(10);
lcd.noCursor();
}
Nenhum comentário:
Postar um comentário