Hello, I'm trying to use with a 3x4 Keypad in a 16x2 i2c LCD (arduino uno), but I'm getting some issues with my code. I want to clarify that I'm a beginner with Arduino and this is one of my first attempts at making something complex that works properly, and my english may not be the best so I appreciate anyone that could help me on this. And also, I'm just starting to code, so most parts of the code here are actually gathered from projects on the internet that I found, but some parts I needed to modify to make it how I want to be and also due to no code alike on the internet, which caused the issues...
The code is supposed to let you type in all 32 spaces on the 16x2 LCD using a 3x4 Keypad, and after you try to type one more character than its limit, the LCD should clear and go back to the first spot (thus being column = 0 and row = 0).
- At first, I used a simple code to make the keypad change rows when it hits the character limit on the current row using a sum variable with both columns and rows values, but it caused to not print properly the last character in the second row and last column of the LCD. It actually prints the character, but the code tells the LCD to instantly clear and go back to the first column in the first row which is wrong because it should actually only reset the LCD when you try to type in the "17" space column (type more characters than it already exists in the LCD screen), so the sum variable should use the value 18 instead (because the sum variable will use the column value + row 1 value, so 17 would be after 16 in the LCD and then results in 18), but it doesn't work like that because the LCD will change the cursor back to the first column in the first row (0, 1) if you try to type more than that and it will start to overwrite the text.
the first code was this:
#include <Keypad.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
int cursorColumn = 0;
int cursorRow = 0;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8};
byte pin_column[COLUMN_NUM] = {7, 6, 5};
Keypad customKeypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
void setup(){
Serial.begin(9600); //INICIALIZA A SERIAL
lcd.backlight();
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
}
void loop(){
char customKey = customKeypad.getKey();
int sum;
sum = cursorColumn + cursorRow;
if (customKey){
Serial.println(customKey);
lcd.setCursor(cursorColumn, cursorRow);
lcd.print(customKey);
cursorColumn++;
if(cursorColumn == 16) {
cursorColumn = 0;
cursorRow = 1;
lcd.setCursor(cursorColumn, cursorRow);
}
sum++;
if(sum == 17) {
lcd.clear();
cursorColumn = 0;
cursorRow = 0;
lcd.setCursor(cursorColumn, cursorRow);
}
}
}
- Then I tried making another version which uses the waitForKey(); function. So basically, almost the same code but when the sum is 17 it should call this function and everything would be stopped and the text would still be there, and only after you press any key it should reset the LCD, clearing the screen and rows and columns values.
This time I really don't know what's wrong with the code because it simple doesn't stop everything as it should do with the waitForKey function, when you reach 17 it does nothing and you can continue to type, but as I said before, it will make the cursor go to {0, 1} and you will start to overwrite the text on the current row.
Please tell me what am I doing wrong here.
the last code is this:
#include <Keypad.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
const int ROW_NUM = 4;
const int COLUMN_NUM = 3;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
int cursorColumn = 0;
int cursorRow = 0;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pin_rows[ROW_NUM] = {11, 10, 9, 8};
byte pin_column[COLUMN_NUM] = {7, 6, 5};
Keypad customKeypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
void setup(){
lcd.backlight();
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
}
void loop(){
char customKey = customKeypad.getKey();
if(customKey){
Serial.println(customKey);
lcd.setCursor(cursorColumn, cursorRow);
lcd.print(customKey);
cursorColumn++;
if(cursorColumn == 16) {
cursorColumn = 0;
cursorRow = 1;
lcd.setCursor(cursorColumn, cursorRow);
}
}
}
void waitforKey(){
char keyPress = customKeypad.waitForKey();
int sum;
sum = cursorColumn + cursorRow;
sum++;
if(sum == 17) {
customKeypad.waitForKey();
}
if(keyPress){
lcd.clear();
cursorColumn = 0;
cursorRow = 0;
lcd.setCursor(cursorColumn, cursorRow);
}
}
Schematic: