int input1 = 17;
int input2 = 5;
int input3 = 18;
int input4 = 22;
int input5 = 0;
int input6 = 4;
int input7 = 15;
int input8 = 16;
int output1 = 27;
int output2 = 26;
int output3 = 25;
int output4 = 33;
int output5 = 32;
int add = 19;
int sub = 2;
long op;
void setup() {
pinMode(input1, INPUT);
pinMode(input2, INPUT);
pinMode(input3, INPUT);
pinMode(input4, INPUT);
pinMode(input5, INPUT);
pinMode(input6, INPUT);
pinMode(input7, INPUT);
pinMode(input8, INPUT);
pinMode(input2, INPUT);
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
Serial.begin(115200);
Serial.println("\n\nENTER NUMBER ONLY!");
Serial.println("Select either [1] Addition or [2] Subtraction");
Serial.parseInt();
}
void loop() {
while (Serial.available() == 0) {
}
op = Serial.readString().toInt();
Serial.println(op);
outLeds(op, 2);
//Perform arithmetic operation
switch (op) {
case 1:
result = input1 + input2;
break;
case 2:
result = input1 - input2;
break;
default:
Serial.println("Invalid input.");
}
}
//Declare and assign led pins
/*const int LEDS[16] = {27, 26, 23, 22, 21, 19, 18, 5, 17, 16, 4, 0, 2, 15, 12, 14};
const int IN_LEDS[3][4] = {{23, 22, 21, 19}, {18, 5, 17, 16}, {27, 26, 26, 27}};
const int OUT_LEDS[6] = {4, 0, 2, 15, 12, 14};
//Variables for arithmetic operation
long input1, input2, result, op;
void setup() {
Serial.begin(115200);
//Mark all 16 Led Pins as OUTPUT
for (int a = 0; a < 16; a++) {
pinMode(LEDS[a], OUTPUT);
}
}
//FOR INPUT 1 LEDs, INPUT 2 LEDs, and Operator LEDs
//Funtion for converting integer into byte array, byte array
//Match Led output pins into byte array, turn on Led when byte is 1
void outLeds(int num, int index) {
String binaryStr = String( num, BIN);
binaryStr = ("0000" + binaryStr).substring(binaryStr.length());
for (int a = 0; a < 4; a++) {
digitalWrite(IN_LEDS[index][a], binaryStr.charAt(a) == '1' ? HIGH : LOW);
}
}
//FOR OUTPUT LEDs
void resultLeds(int num) {
String binaryStr = String( num, BIN);
binaryStr = ("000000" + binaryStr).substring(binaryStr.length());
for (int a = 0; a < 6; a++) {
digitalWrite(OUT_LEDS[a], binaryStr.charAt(a) == '1' ? HIGH : LOW);
}
}
void loop() {
Serial.println("Input any key to start... ");
while (Serial.available() == 0)
{
}
//Wait for any input in the terminal to start
Serial.readString();
//Turn OFF all LEDs
for (int a = 0; a < 16; a++) {
digitalWrite(LEDS[a], LOW);
}
Serial.print("Enter the first input: ");
while (Serial.available() == 0)
{
}
//Wait for input 1
input1 = Serial.readString().toInt();
Serial.println(input1);
//Call function to turn on LEDs for Input 1
outLeds(input1, 0);
Serial.print("Enter the second input: ");
while (Serial.available() == 0)
{
}
//Wait for input 2
input2 = Serial.readString().toInt();
Serial.println(input2);
//Call function to turn on LEDs for Input 2
outLeds(input2, 1);
Serial.println("\nOperation:");
Serial.println("[1] Addition");
Serial.println("[2] Subtraction");
Serial.print("Choose your operation: ");
while (Serial.available() == 0) {
}
//Wait for operator input
op = Serial.readString().toInt();
Serial.println(op);
outLeds(op, 2);
//Perform arithmetic operation
switch (op) {
case 1:
result = input1 + input2;
break;
case 2:
result = input1 - input2;
break;
default:
Serial.println("Invalid input.");
}
//Display the result of the operation
Serial.print("Result: ");
Serial.println(result);
//Call funtion to turn on LEDs for Output
resultLeds(result);
}*/