/*
This sketch converts a Decimal number into a Hexadecimal number.
The Decimal number is fed to the Arduino through a 4x4 Keypad.
A function then converts this Decimal number to its Hexadecimal
equivalent. These numbers are displayed on a Liquid Crystal Display
and Serial Monitor.
This program is made by Shreyas for Electronics Champ YouTube Channel.
Please subscribe to this channel. Thank You.
*/
// including the libraries
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
// initialize the variables
const byte ROWS = 4; //Four rows of Keypad
const byte COLS = 4; //Four columns of Keypad
char key;
String decimalNum;
long decimalNumber;
String hexNumber;
//Define the symbols on the buttons of the keypad
char Keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
// create keypad and LCD objects
Keypad myKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// start Serial communication
Serial.begin(9600);
Serial.println("Electronics Champ");
// initialize the lcd
lcd.init();
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Electronics");
lcd.setCursor(0, 1);
lcd.print("Champ");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(0);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(0);
}
void loop() {
key = myKeypad.getKey();
if (key) {
if (key != 'A' and key != 'B' and key != 'C' and key != 'D' and key != '*' and key != '#') {
decimalNum = decimalNum + String(key);
decimalNumber = decimalNum.toInt();
//Converts Decimal to Hexadecimal
hexNumber = convertDecimalToHex(decimalNumber);
//Prints the Decimal and the Hexadecimal numbers on the Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(decimalNumber);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(hexNumber);
//Prints the Decimal number on the Serial Monitor
Serial.print("Decimal: ");
Serial.print(decimalNumber);
Serial.print(" ");
//Prints the Hexadecimal number on the Serial Monitor
Serial.print("Hexadecimal: ");
Serial.println(hexNumber);
}
}
//Clears the numbers
if (key == 'C') {
decimalNum = "";
decimalNumber = 0;
hexNumber = "0";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(decimalNumber);
lcd.setCursor(0, 1);
lcd.print("H: ");
lcd.setCursor(3, 1);
lcd.print(hexNumber);
}
}
//This function converts a Decimal number to a Hexadecimal number
String convertDecimalToHex(long n) {
String hexNum;
long remainder;
while (n > 0) {
remainder = n % 16;
n = n / 16;
if (remainder < 10) {
hexNum = String(remainder) + hexNum;
}
else {
switch (remainder) {
case 10:
hexNum = "A" + hexNum;
break;
case 11:
hexNum = "B" + hexNum;
break;
case 12:
hexNum = "C" + hexNum;
break;
case 13:
hexNum = "D" + hexNum;
break;
case 14:
hexNum = "E" + hexNum;
break;
case 15:
hexNum = "F" + hexNum;
break;
}
}
}
return hexNum;
}
Comments