Components and supplies
Breadboard (generic)
I2C Module
4x4 Keypad
Alphanumeric LCD, 16 x 2
Arduino UNO
Jumper wires (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
Code
1/* 2 3 This sketch converts a Decimal number into a Hexadecimal number. 4 The Decimal number is fed to the Arduino through a 4x4 Keypad. 5 A function then converts this Decimal number to its Hexadecimal 6 equivalent. These numbers are displayed on a Liquid Crystal Display 7 and Serial Monitor. 8 9 This program is made by Shreyas for Electronics Champ YouTube Channel. 10 Please subscribe to this channel. Thank You. 11 12*/ 13 14// including the libraries 15#include <LiquidCrystal_I2C.h> 16#include <Keypad.h> 17#include <Wire.h> 18 19// initialize the variables 20const byte ROWS = 4; //Four rows of Keypad 21const byte COLS = 4; //Four columns of Keypad 22char key; 23String decimalNum; 24long decimalNumber; 25String hexNumber; 26 27//Define the symbols on the buttons of the keypad 28char Keys[ROWS][COLS] = { 29 30 {'1', '2', '3', 'A'}, 31 {'4', '5', '6', 'B'}, 32 {'7', '8', '9', 'C'}, 33 {'*', '0', '#', 'D'} 34 35}; 36 37byte rowPins[ROWS] = {2, 3, 4, 5}; 38byte colPins[COLS] = {6, 7, 8, 9}; 39 40// create keypad and LCD objects 41Keypad myKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS); 42LiquidCrystal_I2C lcd(0x27, 16, 2); 43 44void setup() { 45 46 // start Serial communication 47 Serial.begin(9600); 48 Serial.println("Electronics Champ"); 49 50 // initialize the lcd 51 lcd.init(); 52 lcd.begin(16, 2); 53 lcd.clear(); 54 lcd.backlight(); 55 lcd.setCursor(0, 0); 56 lcd.print("Electronics"); 57 lcd.setCursor(0, 1); 58 lcd.print("Champ"); 59 delay(3000); 60 lcd.clear(); 61 lcd.setCursor(0, 0); 62 lcd.print("D: "); 63 lcd.setCursor(3, 0); 64 lcd.print(0); 65 lcd.setCursor(0, 1); 66 lcd.print("H: "); 67 lcd.setCursor(3, 1); 68 lcd.print(0); 69 70} 71 72void loop() { 73 74 key = myKeypad.getKey(); 75 76 if (key) { 77 78 if (key != 'A' and key != 'B' and key != 'C' and key != 'D' and key != '*' and key != '#') { 79 80 decimalNum = decimalNum + String(key); 81 decimalNumber = decimalNum.toInt(); 82 83 //Converts Decimal to Hexadecimal 84 hexNumber = convertDecimalToHex(decimalNumber); 85 86 //Prints the Decimal and the Hexadecimal numbers on the Display 87 lcd.clear(); 88 lcd.setCursor(0, 0); 89 lcd.print("D: "); 90 lcd.setCursor(3, 0); 91 lcd.print(decimalNumber); 92 lcd.setCursor(0, 1); 93 lcd.print("H: "); 94 lcd.setCursor(3, 1); 95 lcd.print(hexNumber); 96 97 //Prints the Decimal number on the Serial Monitor 98 Serial.print("Decimal: "); 99 Serial.print(decimalNumber); 100 Serial.print(" "); 101 102 //Prints the Hexadecimal number on the Serial Monitor 103 Serial.print("Hexadecimal: "); 104 Serial.println(hexNumber); 105 106 } 107 108 } 109 110 //Clears the numbers 111 if (key == 'C') { 112 113 decimalNum = ""; 114 decimalNumber = 0; 115 hexNumber = "0"; 116 lcd.clear(); 117 lcd.setCursor(0, 0); 118 lcd.print("D: "); 119 lcd.setCursor(3, 0); 120 lcd.print(decimalNumber); 121 lcd.setCursor(0, 1); 122 lcd.print("H: "); 123 lcd.setCursor(3, 1); 124 lcd.print(hexNumber); 125 126 } 127 128} 129 130//This function converts a Decimal number to a Hexadecimal number 131String convertDecimalToHex(long n) { 132 133 String hexNum; 134 long remainder; 135 136 while (n > 0) { 137 138 remainder = n % 16; 139 n = n / 16; 140 141 if (remainder < 10) { 142 hexNum = String(remainder) + hexNum; 143 } 144 145 else { 146 147 switch (remainder) { 148 149 case 10: 150 hexNum = "A" + hexNum; 151 break; 152 153 case 11: 154 hexNum = "B" + hexNum; 155 break; 156 157 case 12: 158 hexNum = "C" + hexNum; 159 break; 160 161 case 13: 162 hexNum = "D" + hexNum; 163 break; 164 165 case 14: 166 hexNum = "E" + hexNum; 167 break; 168 169 case 15: 170 hexNum = "F" + hexNum; 171 break; 172 173 } 174 175 } 176 177 } 178 179 return hexNum; 180 181} 182
Downloadable files
Schematic
Tinkercad software is used to design the schematic.
Schematic
Comments
Only logged in users can leave comments