Hello i have a problem with this project im trying to finish, it's about typing from a Keypad and alphanumeric text typed will be display in lcd screen, i want to store every wrote char into a String, size of lcd display max 15 chars i guess.
This is the code, its not complete but it only needs some more "switch cases" for the other keypad buttons, and an attempt i made to solve this but it does not work as intended. i might be able to upload the code if needed.
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
#include <Keypad.h>
const byte filas = 4; //number of rows
const byte columnas = 4; //Number of columns
//Define a matrix 4x4 with pos of rows and columns
char matriz[filas][columnas] =
{
{ '1', '2', '3', 'A'},
{ '4', '5', '6', 'B'},
{ '7', '8', '9', 'C'},
{ '*', '0', '#', 'D'},
};
byte pinesFilas[filas] = {22, 24, 26, 28}; //key pad rows pins
byte pinesColumnas[columnas] = {30, 32, 34, 36}; //Keypad column pins
//Initialize the keyboard with the number of rows, columns, the Arduino pins used and the matrix
Keypad teclado = Keypad( makeKeymap(matriz), pinesFilas, pinesColumnas, filas, columnas);
//Variables to control cursor position.
int posX = 0;
int posY = 0;
//Variables to control keys
char presionando; // holding key
int veces_presionada = 0; // times_pressed
int mayuscula_activada = 0; //mayus_ON
int mostrar_cursorPantalla = 1; //show_cursor on screen
int contador = 0; //counter
// Variables for the control of the counter, since no delay () is used in this code
unsigned long tiempo_anterior = 0;
int periodo = 500; //500 millisecconds
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); //Initializing screen
lcd.setCursor(3, 0);
lcd.print("Escribe un");
lcd.setCursor(4, 1);
lcd.print("mensaje");
delay(2000);
lcd.clear();
}
void loop() {
char tecla_presionada = teclado.getKey(); //Store the key pressed
/*String outputString="";// This comented seccion is my attempt to save the text but it only saves
keypad value not the written text.
if(tecla_presionada){
Serial.println(tecla_presionada);
outputString.concat(tecla_presionada);
//lcd.write(Serial.read());
Serial.println(outputString);
}*/
if (millis() > tiempo_anterior + periodo) //Conditional to evaluate if 500 milliseconds passed
{
tiempo_anterior = millis();
// If 500 milliseconds passed and there was a key pressed, the counter increases by +1
if (veces_presionada > 0)
{
contador++;
if (contador == 3) // If the counter reaches 3, the cursor advances a position
{
posX++;
veces_presionada = 0;
}
}
mostrar_cursorPantalla++; // Variable for the control of the blinking of the cursor
if (mostrar_cursorPantalla > 1)
{
mostrar_cursorPantalla = 0;
}
}
if (mostrar_cursorPantalla == 1) // Show the cursor on the screen
{
lcd.setCursor(posX, posY);
lcd.cursor();
}
if (mostrar_cursorPantalla == 0) // Hide the cursor on the screen
{
lcd.setCursor(posX, posY);
lcd.noCursor();
}
if (posX > 15) // The position in X can not be greater than 15 (the X axis goes from 0 to 15)
{
posX = 0;
posY++;
}
if (posX < 0) // The position in X can not be less than 0
{
posX = 0;
}
if (posY > 1) // The position in Y can not be greater than 1 (the Y axis goes from 0 to 1)
{
posY = 0;
}
switch (tecla_presionada) // Switch-case according to the key pressed
{
case 'A': // The A key is the spacebar. Increase the cursor position by +1
posX++;
veces_presionada = 0;
// data[tecla_presionada];
break;
case 'B': // The B key is used to erase the previous character written
posX--;
lcd.setCursor(posX, posY);
lcd.print(" ");
veces_presionada = 0;
break;
case 'C': // The C key erases EVERYTHING written on the screen and positions the cursor at (0,0)
lcd.clear();
posX = 0;
posY = 0;
veces_presionada = 0;
break;
case 'D': // The D key moves the cursor on the Y axis
posY++;
veces_presionada = 0;
break;
case '0':
// Conditional so that the cursor advances if a different key was previously pressed
if (presionando != '0' && veces_presionada > 0)
{
posX++;
veces_presionada = 0;
}
lcd.setCursor(posX, posY);
veces_presionada++;
contador = 0;
presionando = '0';
if (veces_presionada > 5)
{
veces_presionada = 1;
}
if (veces_presionada == 1)
{
lcd.print('+');
}
if (veces_presionada == 2)
{
lcd.print('-');
}
if (veces_presionada == 3)
{
lcd.print('*');
}
if (veces_presionada == 4)
{
lcd.print('/');
}
if (veces_presionada == 5)
{
lcd.print('0');
}
break;
case '*': // The * key activates and deactivates the capital letters
mayuscula_activada++;
if (mayuscula_activada > 1)
{
mayuscula_activada = 0;
}
break;
case '#':
// Conditional so that the cursor advances if a different key was previously pressed
if (presionando != '#' && veces_presionada > 0)
{
posX++;
veces_presionada = 0;
}
lcd.setCursor(posX, posY);
veces_presionada++;
contador = 0;
presionando = '#';
if (veces_presionada > 5)
{
veces_presionada = 1;
}
if (veces_presionada == 1)
{
lcd.print('>');
}
if (veces_presionada == 2)
{
lcd.print('<');
}
if (veces_presionada == 3)
{
lcd.print('=');
}
if (veces_presionada == 4)
{
lcd.print('!');
}
if (veces_presionada == 5)
{
lcd.print('#');
}
break;
case '1':
// Conditional so that the cursor advances if a different key was previously pressed
if (presionando != '1' && veces_presionada > 0)
{
posX++;
veces_presionada = 0;
}
lcd.setCursor(posX, posY);
veces_presionada++;
contador = 0;
presionando = '1';
if (veces_presionada > 5)
{
veces_presionada = 1;
}
if (veces_presionada == 1)
{
lcd.print('(');
}
if (veces_presionada == 2)
{
lcd.print(')');
}
if (veces_presionada == 3)
{
lcd.print('.');
}
if (veces_presionada == 4)
{
lcd.print(',');
}
if (veces_presionada == 5)
{
lcd.print('1');
}
break;
case '2':
// Conditional so that the cursor advances if a different key was previously pressed
if (presionando != '2' && veces_presionada > 0)
{
posX++;
veces_presionada = 0;
}
lcd.setCursor(posX, posY);
veces_presionada++;
contador = 0;
presionando = '2';
if (veces_presionada > 4)
{
veces_presionada = 1;
}
if (veces_presionada == 1)
{
if (mayuscula_activada == 1 || (posX == 0 && posY == 0))
{
lcd.print('A');
}
else
{
lcd.print('a');
}
}
if (veces_presionada == 2)
{
if (mayuscula_activada == 1 || (posX == 0 && posY == 0))
{
lcd.print('B');
}
else
{
lcd.print('b');
}
}
if (veces_presionada == 3)
{
if (mayuscula_activada == 1 || (posX == 0 && posY == 0))
{
lcd.print('C');
}
else
{
lcd.print('c');
}
}
if (veces_presionada == 4)
{
lcd.print('2');
}
break;
}
}