Hello.
I have written a code with LCD and 4x4 matrix keyboard. I made that I can type a numer that I want, on the second row of the LCD. I also made that if I press the "A" I can delite the last number. But now I want to save the number that is written on the LCD (in the integer for example), so i could use that number later in the program. And I got no idea how to do it. Any advises?
save the number as you build it on screen in a null terminated char buffer for example and then use atoi() or atol() to transform that string into a number
may be something like this (typed here totally untested)
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte NB_COLS = 20; // number of LCD columns
char charr;
int currentCol = 0;
int currentLine = 1;
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[4] = {A0, A1, A2, A3};
byte colPins[4] = {A4, A5, A6, A7};
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
const byte maxInputSize = 20;
char inputBuffer[maxInputSize + 1];
byte currentBufferPosition = 0;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.print("ok");
delay(100);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("NUMBER:");
char keyPressed = myKeypad.getKey();
if (keyPressed) {
charr = keyPressed;
if ((charr != '*') && (charr != '#') && (charr != 'A') &&
(charr != 'B') && (charr != 'C') && (charr != 'D')) {
//*** NOTE WOULD BE GOOD TO CHECK YOU FIT ON THE LINE
if (currentCol < NB_COLS) {
lcd.setCursor(currentCol++, currentLine);
lcd.print(charr);
if (currentBufferPosition < maxInputSize) {
inputBuffer[currentBufferPosition++] = charr;
inputBuffer[currentBufferPosition] = '\0';
}
}
} else if (charr == 'A') {
if (currentCol != 0) {
currentCol--;
lcd.setCursor(currentCol, currentLine);
lcd.write(' ');
}
if (currentBufferPosition != 0) inputBuffer[--currentBufferPosition] = '\0';
}
Serial.print(F("My inputBuffer holds: ["));
Serial.print(inputBuffer);
Serial.println(F("]"));
// calling atol() on the inputBuffer would give you a value you can play with
}
}
(I changed the console speed to 115200, no need to go super slow)