#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' },
{ '*', '0', '#' }
};
byte colPins[COLS] = {4,5,6}; // Pins connected to C1, C2, C3, C4
byte rowPins[ROWS] = {0,1,2,3}; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String x = "";
void setup() {
lcd.begin(16,2);
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
if ((key!=NO_KEY)&&(key!='*')&&(key!='#')){
lcd.setCursor(0,1);
lcd.print(key);
x += key;
if (x.length()<=16){
String y = Sort(x);
lcd.setCursor(0,0);
lcd.print(y);
}
else{
x = "";
lcd.clear();
}
}
}
String Sort(String x){
if (x.length() <= 1){
return x;
}
else{
for(int j = 1; j < x.length(); j++){
int key = x[j];
int i = j - 1;
while ((i>=0)&&(x[i])>key){
x[i+1] = x[i];
i -= 1;
}
x[i+1] = key;
}
}
return x;
}