I want to make a calculator using an adder and a xor (LS283),(LS86) with 12 Leds. Eight at a time are used to show me the input in binary, so the first four give me the first part and the second four give me the second part of the equation andhe last four leds show me the result, also in binary. I have no problems with the circuit but with my code. It does not work.
int A1Pin = 5;
int A2Pin = 3;
int A3Pin = 14;
int A4Pin = 12;
int B1Pin = 6;
int B2Pin = 2;
int B3Pin = 15;
int B4Pin = 11;
int LED1Pin = 2;
int LED2Pin = 3;
int LED3Pin = 4;
int LED4Pin = 5;
int LED5Pin = 9;
int LED6Pin = 10;
int LED7Pin = 11;
int LED8Pin = 12;
int LEDs1Pin = 4;
int LEDs2Pin = 1;
int LEDs3Pin = 13;
int LEDs4pin = 10;
long A; // first equation part
long B; // second equation part
char operation; // The particular operation
long result; // result
void setup()
{
Serial.begin(9600);
Serial.println("calculate:");
pinMode(A1Pin, OUTPUT);
pinMode(A2Pin, OUTPUT);
pinMode(A3Pin, OUTPUT);
pinMode(A4Pin, OUTPUT);
pinMode(B1Pin, OUTPUT);
pinMode(B2Pin, OUTPUT);
pinMode(B3Pin, OUTPUT);
pinMode(B4Pin, OUTPUT);
pinMode(LED1Pin, INPUT);
pinMode(LED2Pin, INPUT);
pinMode(LED3Pin, INPUT);
pinMode(LED4Pin, INPUT);
pinMode(LED5Pin, INPUT);
pinMode(LED6Pin, INPUT);
pinMode(LED7Pin, INPUT);
pinMode(LED8Pin, INPUT);
}
void loop() {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
while(Serial.available() > 0) {
A = Serial.parseInt();
operation = Serial.read();
B = Serial.parseInt();
calculate(); // Custom function to perform the calculations
Serial.print("result = ");
Serial.println(result);
Serial.println("done...");
Serial.println();
}
}
void calculate() {
if (operation == '+') {
result = A + B;
}
else if (operation == '-') {
result = A - B;
}
// input a as four separate bits
bool a0 = digitalRead(A1Pin);
bool a1 = digitalRead(A2Pin);
bool a2 = digitalRead(A3Pin);
bool a3 = digitalRead(A4Pin);
//input b as four sepatate bits
bool b0 = digitalRead(B1Pin);
bool b1 = digitalRead(B2Pin);
bool b2 = digitalRead(B3Pin);
bool b3 = digitalRead(B4Pin);
bool s0 = a0 ^ b0;
bool c0 = a0 & b0;
bool c1 = a1 & b1;
bool c2 = a2 & b2;
bool c3 = a3 & b3;
bool s1 = (a0 ^ b0) ^ c0;
bool s2 = (a1 ^ b1) ^ c1;
bool s3 = (a2 ^ b2) ^ c2;
bool s4 = (a3 ^ b3) ^ c3;
digitalWrite(LEDs1Pin, s0);
digitalWrite(LEDs2Pin, s1);
digitalWrite(LEDs3Pin, s3);
digitalWrite(LEDs4pin, s4);
}
Please read the post at the start of any forum , entitled "How to use this Forum".
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Please do not use Fritzy to make your diagram.
Please include component names and pin labels.
What model Arduino are you using?
If you have configured a digital pin as an INPUT you can only digitalRead from it and not digitalWrite to it. Conversely if the pin is configured as an OUTPUT you can only digitalWrite to it.
You seem to have the pins configured backwards. the A and B pins should be inputs and the LEDs pins should be outputs.
Yep, true. But in this example he's simply got INPUT and OUTPUT backwards. Yes, reading from an output pin does something and writing to an input pin does something, but nothing that the original poster wants to do!
Waggling the pull up resistor on an input pin can cause a connected LED to go from dim to very dim, so yes, something the original poster wanted to do (without the "dim" bit)
What I had in mind:
I want a kind of calculator with an adder and a XOR, with 12 leds, the first 4 leds should display the first value in binary, and the second 4 leds display the second value in binary, with the adder and Xor a sum of both eqations is added up (but just summation and subtraction like A+B=S, A-B=S, A representing the first 4 Leds and B representing the Second 4 Leds and S the sum of both in 4 leds). I hope it is clearer now.
Unless I'm missing something, the original code doesn't make sense. The operands of the colcualtion are first parsed from serial, and then a binary representation is supposedly read from 8 pins, but there's no way the serial input is transposed to the pins and there are only 4 pins connected to the Nano.