#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ctype.h>
#define ROWS 4
#define COLS 4
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
int Original = 0;
int Converted = 0;
String OriginalChoices[4] = {" BIN"," DEC"," OCT"," HEX"};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin(16, 2); // initialize the LCD
lcd.backlight(); // turn on the backlight
lcd.print(" Number System ");
lcd.setCursor(0,1);
lcd.print(" Converter");
delay(1500);
lcd.clear();
lcd.print(OriginalChoices[0]);
lcd.print(" to");
}
void loop() {
Menu();
GetUserInput();
}
void Menu() {
char choice = keypad.getKey();
//Ask user what is the number system to be use
while(choice != '5') {
choice = keypad.getKey();
if(choice == '6' && Original < 3){
lcd.clear();
lcd.print(OriginalChoices[++Original]);
lcd.print(" to");
}
else if(choice == '4' && Original > 0) {
lcd.clear();
lcd.print(OriginalChoices[--Original]);
lcd.print(" to");
}
}
//Ask user to what to convert
choice = keypad.getKey();
while(choice != '5') {
choice = keypad.getKey();
if(choice == '6' && Converted < 3){
Converted++;
if(Converted == Original) Converted++;
if(Converted > 3) Converted = Original-1;
lcd.clear();
lcd.print(OriginalChoices[Original]);
lcd.print(" to");
lcd.print(OriginalChoices[Converted]);
}
else if(choice == '4' && Converted > 0) {
Converted--;
if(Converted == Original) Converted--;
if(Converted < 0) Converted = Original+1;
lcd.clear();
lcd.print(OriginalChoices[Original]);
lcd.print(" to");
lcd.print(OriginalChoices[Converted]);
}
//Converted = (Converted == Original)?(Converted < 2 )? Converted+2 : Converted = 0 : Converted+1;
}
}
void GetUserInput() {
char input = keypad.getKey();
while(input != NO_KEY) {
}
}
void SampleKeyInput() {
char key = keypad.getKey();
if (key != NO_KEY ) {
lcd.clear();
lcd.print(key);
}
}