Computer Project
Computer Project
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
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)
Some connections may be loose: in this case the LCD shows a row of filled rectangular boxes.
Please adjust all pins if this happens.
*/
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
hd44780_I2Cexp lcd;
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");
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