0% found this document useful (0 votes)
2 views

Computer Project

This document outlines a project by Souvik Banerjee that involves creating a machine language translator using Arduino to convert decimal inputs to binary and vice versa. The project emphasizes the significance of binary and decimal systems in computing and provides a detailed explanation of the algorithm, source code, and operational functionality. It concludes by highlighting the educational value and potential for future enhancements of the translator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer Project

This document outlines a project by Souvik Banerjee that involves creating a machine language translator using Arduino to convert decimal inputs to binary and vice versa. The project emphasizes the significance of binary and decimal systems in computing and provides a detailed explanation of the algorithm, source code, and operational functionality. It concludes by highlighting the educational value and potential for future enhancements of the translator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Project

Machine Language Translator


by Souvik Banerjee

Device:
Using Arduino, we have coded a physical translator to translate decimal inputs
to binary, and binary inputs to decimal.

Use:
Binary and decimal systems are fundamental to computing and digital
technology. The binary system, consisting of only two digits (0 and 1), is the
backbone of computer operations, as it aligns with the on-off states of
electronic circuits. It is used to represent data, perform calculations, and
execute logical operations. In contrast, the decimal system, based on ten digits
(0-9), is the primary system humans use for everyday calculations. Translating
between these systems is crucial for human-computer interaction, enabling us
to input and interpret data in a familiar format while allowing computers to
process it efficiently in binary.

How it works:
1. Input is received (binary/decimal).
2. The program identifies the type of input.
3. The conversion algorithm processes the input.
4. The output is displayed.
Pictures

Shows the interface, allows switching between binary and decimal output

An example of Decimal input(top line) and Binary output(bottom line)

An example of Binary input(bottom line) and Decimal output(top line)


Algorithm

Initialization
1. Include Libraries: Import necessary libraries for keypad, LCD, and math
operations.
2. Define Variables and Constants:
 Configure the keypad (rows and columns, key mappings, and pins).
 Declare variables to hold user input (activeString), binary value (bin),
decimal value (dec), and the current input mode (decimalInput).
3. Setup Devices:
 Initialize serial communication at 9600 baud.
 Initialize the LCD display with dimensions (16x2) and set default
messages:
 "* for RESET" (top row).
 "# to SWITCH MODES" (bottom row).

Main Loop
1. Key Detection: Continuously monitor the keypad for user input. If no key
is pressed, retry.
2. Special Keys Handling:
o If * is pressed:
 Clear all inputs (activeString, dec, bin).
 Display "Cleared!" on the LCD for 1 second.
 Reset to initial state.
o If # is pressed:
 Clear all inputs.
 Toggle the input mode (decimalInput).
 Display the new mode ("DECIMAL INPUT!" or "BINARY
INPUT!") on the LCD for 1 second.
3. Input Validation:
o If in binary mode and the key pressed is not 0 or 1, ignore the
input and retry.
4. Append Key to Input:
o Concatenate the valid key to activeString.
5. Perform Conversion:
o If in decimal mode:
 Convert activeString (decimal) to its binary equivalent using
the helper function decToBin().
o If in binary mode:
 Convert activeString (binary) to its decimal equivalent using
the helper function binToDec().
6. Display Results:
o Display the decimal value (dec) on the top row of the LCD.
o Display the binary value (bin) on the bottom row of the LCD.
Source Code:
/ Requirements!
#include <math.h>
#include <Wire.h>
#include <Keypad.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

/*
Membrane keypad connected on pins 2 though 9 (inclusive)
(left side (with *) connects to pin 9, right side (with D) connects to 2)

I2C LCD: VCC to 5V, GND to GND, SCL to A5, SDA to A4

Some connections may be loose: in this case the LCD shows a row of filled rectangular boxes.
Please adjust all pins if this happens.
*/

const byte ROWS = 4; // four rows


const byte COLS = 4; // four columns

char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

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

//Create an object of keypad


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

hd44780_I2Cexp lcd;

// Represents current user input


String activeString = "";
// Binary value of user input
String bin = "";
int dec = 0;

bool decimalInput = true; // Monitors current input type

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("* for RESET");
lcd.setCursor(0, 1);
lcd.print("# to SWITCH MODES");
lcd.setCursor(0, 0);
}

void loop() {
mark: // recursively calling loop() calls setup(), which causes issues.
char c = keypad.getKey();
if (!c) {
goto mark; // no other option!
}

Serial.println(c);

if (c == '*') {
// RESET Case
activeString = "";
dec = 0;
bin = "";
lcd.clear();
lcd.print("Cleared!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
Serial.println("C1");
goto mark;
}
if (c == '#') {
// SWITCH MODES Case
activeString = "";
dec = 0;
bin = "";
decimalInput = !decimalInput;
lcd.clear();
lcd.setCursor(0, 0);
if (decimalInput) {
lcd.print("DECIMAL INPUT!");
}
else {
lcd.print("BINARY INPUT!");
}
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
Serial.println("C2");
goto mark;
}
if (!decimalInput && (c != '0' && c != '1')) {
// In binary mode, reject non-0 and non-1 input
goto mark;
}
// Input is valid. Append it to current input.
activeString = activeString + c;
Serial.println(activeString);
lcd.setCursor(0, 0);
Serial.println("P2");

// Make the required conversions


if (decimalInput) {
dec = activeString.toInt();
bin = decToBin(dec);
}
else {
bin = activeString;
dec = binToDec(bin);
}
// All Serial prints are for debugging
Serial.println(dec);
Serial.println(bin);

lcd.setCursor(0, 0);
lcd.clear();
lcd.print(String(dec));
lcd.setCursor(0, 1);
lcd.print(bin);
Serial.println("P3");
delay(1000);

// Helper functions
String decToBin(int n) {
if (n == 1) {
return "1";
}
else {
return decToBin(floor(n / 2)) + String(n % 2);
}
}

int binToDec(String s) {
String num = s;
int position = 0;
int result=0;
Serial.println("Converting " + num + " to decimal!");
int len = num.length();
for(int i= len-1; i>=0;i--){
if(num[i]=='1'){
result+= power(2,position);
Serial.println("Added 2^" + String(position) + "!");
Serial.println("Resut: " + String(result));
}
Serial.println();
position++;
}
return result;
}

// Issues with C++'s native pow() function, so we use a user-defined one instead.
int power(int p, int q) {
if (q == 0) {
return 1;
}
return p * power(p, q-1);
}
Conclusion

In conclusion, this project successfully demonstrates the practical application of


number systems in computing through the development of a decimal-to-binary
and binary-to-decimal translator. By using Arduino , it provides an interactive
and efficient way to perform these conversions, which are essential in the fields
of computer science, electronics, and digital systems.
The project not only highlights the importance of understanding binary logic
but also shows the power of combining hardware and software to create
functional and educational tools. It has applications in learning, debugging, and
basic computational tasks, making it a valuable contribution to both academic
and practical contexts.
Through this project, valuable insights were gained into programming, circuit
design, and problem-solving, laying the groundwork for future innovations.
Enhancements such as additional numeral systems or more user-friendly
interfaces could further expand its utility, ensuring it remains a relevant and
adaptable tool in the digital age.

You might also like