/*
This is a small Arduino project that simulates the T9 typing of old cell
phones with numeric keypads.
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Configurações do LCD / LCD settings
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Configurações do teclado matricial 4x4 / 4x4 Matrix keypad settings
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] = {A0, A1, A2, A3}; // Pinos das linhas / Row pins
byte colPins[COLS] = {10, 9, 8, 7}; // Pinos das colunas / Column pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Mapeamento T9 para cada tecla / T9 mapping for each key
char t9Mapping[10][5] = {
" 0", // tecla 0 / key 0
"1", // tecla 1 / key 1
"ABC2", // tecla 2 / key 2
"DEF3", // tecla 3 / key 3
"GHI4", // tecla 4 / key 4
"JKL5", // tecla 5 / key 5
"MNO6", // tecla 6 / key 6
"PQRS7", // tecla 7 / key 7
"TUV8", // tecla 8 / key 8
"WXYZ9" // tecla 9 / key 9
};
String message = ""; // Armazena a mensagem / Stores the message
unsigned long lastKeyPress = 0; // Tempo da última tecla pressionada / Last key press time
char lastKey = '\0'; // Última tecla pressionada / Last pressed key
int pressCount = 0; // Contador de pressionamentos para o T9 / Press count for T9
const unsigned long pauseTime = 1000; // Tempo de pausa para confirmar a letra / Pause time to confirm letter
bool cursorVisible = true; // Controle para o cursor piscante / Control for blinking cursor
void setup() {
lcd.init(); // Iniciar o LCD / Initialize the LCD
lcd.backlight(); // Ativar luz de fundo / Turn on backlight
lcd.clear(); // Limpar o display / Clear the display
lcd.setCursor(0, 0);
lcd.print("Escreva:"); // Mensagem inicial / Initial message
}
void loop() {
char key = keypad.getKey();
// Atualizar o cursor piscante / Update blinking cursor
if (millis() % 500 < 250) {
if (!cursorVisible) {
lcd.setCursor(message.length(), 1);
lcd.print('_');
cursorVisible = true;
}
} else {
if (cursorVisible) {
lcd.setCursor(message.length(), 1);
lcd.print(' ');
cursorVisible = false;
}
}
if (key) {
unsigned long currentMillis = millis();
// Verifica se a tecla '#' foi pressionada (Backspace) / Check if '#' key was pressed (Backspace)
if (key == '#') {
if (message.length() > 0) {
message.remove(message.length() - 1); // Remove o último caractere / Remove the last character
lcd.setCursor(0, 1);
lcd.print(" "); // Limpa a linha de texto / Clear the text line
lcd.setCursor(0, 1);
lcd.print(message); // Exibe a nova mensagem sem o último caractere / Display the updated message
}
lastKey = '\0'; // Reseta a última tecla / Reset the last key
}
else if (key == lastKey && (currentMillis - lastKeyPress < pauseTime)) {
pressCount++;
int maxPress = strlen(t9Mapping[key - '0']);
if (pressCount >= maxPress) {
pressCount = 0;
}
} else {
if (lastKey != '\0' && (currentMillis - lastKeyPress < pauseTime)) {
// Confirma a letra anterior e adiciona à mensagem / Confirm the previous letter and add to message
message += t9Mapping[lastKey - '0'][pressCount];
lcd.setCursor(0, 1);
lcd.print(message);
}
// Reset para a nova tecla / Reset for the new key
lastKey = key;
pressCount = 0;
}
lastKeyPress = currentMillis;
}
// Confirma a letra se o tempo de pausa passar / Confirm letter if pause time passes
if (lastKey != '\0' && millis() - lastKeyPress >= pauseTime) {
message += t9Mapping[lastKey - '0'][pressCount];
lcd.setCursor(0, 1);
lcd.print(message);
lastKey = '\0'; // Reseta a última tecla / Reset the last key
pressCount = 0;
}
}