Hola, buenos días!
Estoy aprendiendo programación, a mis 46 años, de forma autodidacta, y me surgió un problema que llevo un par de días queriendo resolver.
Estoy trabajando en un teclado matricial 4x4, y lo que quiero conseguir, es emular el teclado TAP de los celulares antiguos que funcionaban de la siguiente manera:
Ejemplo de tecla "1"; esa tecla, si se pulsa 1 vez escribe el "1",si se pulsa 2 veces escribe la letra "A", si se pulsa 3 veces escribe la letra "B" y si se pulsa 4 veces escribe la letra "C", todo en el monitor serial. (Creo que ya entienden el funcionamiento en general del teclado)
Eso lo pude conseguir, con un código rudimentario, que si bien creo no debe ser la forma mas optima, me funciona.
Después programe la tecla "A" para que haga "ESPACIO" y la tecla "C" para el "ENTER". Pero mi problema es que no puedo hacer que la tecla "B" borre, si me equivoco cuando escribo, por eso el código lo hice hasta la tecla 1,2,3, 4, A y C.
Que hace la tecla "B"? en lugar de borrar escribe cuadraditos o algún símbolo...
Voy a continuación a pegar el código, donde se encuentra en la tecla "B" comentadas las opciones que use y todas escriben símbolos. Probé las que me dice la librería keypad y otras que busque en internet, pero no funciona.
Si me pueden orientar, estoy dispuesto a aprender. Un fuerte abrazo a todos!
El Código:
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'A', '3', '2', '1'}, /////OK 1 2 3 A DISTRIBUCION DE TECLADO OK
{'B', '5', '6', '4'},
{'C', '8', '9', '7'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
////////////////////////////////////////TECLA 1
int count = 0;
unsigned long lastKeyPressTime = 0;
unsigned int pulseCount = 0;
///////////////////////////////////////TECLA 2
int count2 = 0;
unsigned long lastKeyPressTime2 = 0;
unsigned int pulseCount2 = 0;
////////////////////////////////////////TECLA 3
int count3 = 0;
unsigned long lastKeyPressTime3 = 0;
unsigned int pulseCount3 = 0;
////////////////////////////////////////TECLA 4
int count4 = 0;
unsigned long lastKeyPressTime4 = 0;
unsigned int pulseCount4 = 0;
/////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
//////////////////////////////////////////////TELAS ESPECIALES
// TECLA A //////////// ESPACIO
if (key == 'A') {
Serial.print(" "); // Agregar un espacio
}
// TECLA B //////////////BORRAR NO FUNCIONA
if (key == 'B') {
Serial.write('\b'); // Borrar el último carácter
//Serial.write(27);
//Serial.print(0xD4);
//Serial.write(0xB2);
}
// TECLA C ///////////ENTER
if (key == 'C') {
Serial.println(); // Agregar un carácter "Enter"
}
////////////////////////////////////////////////////// TECLA 1
if (key == '1') {
if (millis() - lastKeyPressTime > 700) {
pulseCount = count;
count = 1;
} else {
count++;
if (count == 2) {
pulseCount = 2;
} else if (count == 3) {
pulseCount = 3;
} else if (count == 4) {
pulseCount = 4;
}
}
lastKeyPressTime = millis();
}
if (millis() - lastKeyPressTime > 700 && pulseCount > 0) {
if (pulseCount == 1) {
Serial.print("1");
} else if (pulseCount == 2) {
Serial.print("A");
} else if (pulseCount == 3) {
Serial.print("B");
} else if (pulseCount == 4) {
Serial.print("C");
}
pulseCount = 0;
} // Agregar llave de cierre aquí
//////////////////////////////////////////////////// TECLA 2
if (key == '2') {
if (millis() - lastKeyPressTime2 > 700) {
pulseCount2 = count2;
count2 = 1;
} else {
count2++;
if (count2 == 2) {
pulseCount2 = 2;
} else if (count2 == 3) {
pulseCount2 = 3;
} else if (count2 == 4) {
pulseCount2 = 4;
}
}
lastKeyPressTime2 = millis();
}
if (millis() - lastKeyPressTime2 > 700 && pulseCount2 > 0) {
if (pulseCount2 == 1) {
Serial.print("2");
} else if (pulseCount2 == 2) {
Serial.print("D");
} else if (pulseCount2 == 3) {
Serial.print("E");
} else if (pulseCount2 == 4) {
Serial.print("F");
}
pulseCount2 = 0;
}
///////////////////////////////////////////////////TECLA 3
if (key == '3') {
if (millis() - lastKeyPressTime3 > 700) {
pulseCount3 = count3;
count3 = 1;
} else {
count3++;
if (count3 == 2) {
pulseCount3 = 2;
} else if (count3 == 3) {
pulseCount3 = 3;
} else if (count3 == 4) {
pulseCount3 = 4;
}
}
lastKeyPressTime3 = millis();
}
if (millis() - lastKeyPressTime3 > 700 && pulseCount3 > 0) {
if (pulseCount3 == 1) {
Serial.print("3");
} else if (pulseCount3 == 2) {
Serial.print("G");
} else if (pulseCount3 == 3) {
Serial.print("H");
} else if (pulseCount3 == 4) {
Serial.print("I");
}
pulseCount3 = 0;
}
/////////////////////////////////////////////////////////TECLA 4
if (key == '4') {
if (millis() - lastKeyPressTime4 > 700) {
pulseCount4 = count4;
count4 = 1;
} else {
count4++;
if (count4 == 2) {
pulseCount4 = 2;
} else if (count4 == 3) {
pulseCount4 = 3;
} else if (count4 == 4) {
pulseCount4 = 4;
}
}
lastKeyPressTime4 = millis();
}
if (millis() - lastKeyPressTime4 > 700 && pulseCount4 > 0) {
if (pulseCount4 == 1) {
Serial.print("4");
} else if (pulseCount4 == 2) {
Serial.print("J");
} else if (pulseCount4 == 3) {
Serial.print("K");
} else if (pulseCount4 == 4) {
Serial.print("L");
}
pulseCount4 = 0;
}
///////////////////////////////////////////////////////////////
}