/*
This sketch converts a Decimal number into an Octal number.
The Decimal number is fed to the Arduino through a 4x4 Keypad.
A function then converts this Decimal number to its Octal
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 octalNumber;
//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("O: ");
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 Octal
octalNumber = convertDecimalToOctal(decimalNumber);
//Prints the Decimal and the Octal 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("O: ");
lcd.setCursor(3, 1);
lcd.print(octalNumber);
//Prints the Decimal number on the Serial Monitor
Serial.print("Decimal: ");
Serial.print(decimalNumber);
Serial.print(" ");
//Prints the Octal number on the Serial Monitor
Serial.print("Octal: ");
Serial.println(octalNumber);
}
}
//Clears the numbers
if (key == 'C') {
decimalNum = "";
decimalNumber = 0;
octalNumber = "0";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D: ");
lcd.setCursor(3, 0);
lcd.print(decimalNumber);
lcd.setCursor(0, 1);
lcd.print("O: ");
lcd.setCursor(3, 1);
lcd.print(octalNumber);
}
}
//This function converts a Decimal number to an Octal number
String convertDecimalToOctal(long n) {
String octalNum;
int remainder;
while (n > 0) {
remainder = n % 8;
n = n / 8;
octalNum = String(remainder) + octalNum;
}
return octalNum;
}
Comments