Hello, i'm working on this project in wich I had to make a calculator that added two 4 bit binary (or less) numbers, and they told me to use this code in class:
//Number 1
int A =12;
int B=11;
int C=10;
int D=9;
//Number 2
int E=8;
int F=7;
int G=6;
int H=5;
// Result
int Carry = 4;
int I =3;
int J=2;
int K=1;
int L=0;
//boolean variables
bool a,b,c,d,e,f,g,h,i,j,k,l,carry1,carry2,carry3,carry4;
void setup() {
//Number 1
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(C, INPUT);
pinMode(D, INPUT);
//Number 2
pinMode(E, INPUT);
pinMode(F, INPUT);
pinMode(G, INPUT);
pinMode(H, INPUT);
//Result
pinMode(Carry, OUTPUT);
pinMode(I, OUTPUT);
pinMode(J, OUTPUT);
pinMode(K, OUTPUT);
pinMode(L, OUTPUT);
}
void loop() {
//Read input
a = digitalRead(A);
b = digitalRead(B);
c = digitalRead(C);
d = digitalRead(D);
e = digitalRead(E);
f = digitalRead(F);
g = digitalRead(G);
h = digitalRead(H);
//Process
l = (!d && h) || (d && !h);
carry1 = d&&h;
k = (c && g && carry1) || (c && !g && !carry1) || (!c && g && !carry1) || (!c && !g && carry1);
carry2 = (c && g && carry1) || (c && g && !carry1) || (c && !g && carry1) || (!c && g && carry1);
j = (b && f && carry2) || (b && !f && !carry2) || (!b && f && !carry2) || (!b && !f && carry2);
carry3 = (b && f && carry2) || (b && f && !carry2) || (b && !f && carry2) || (!b && f && carry2);
i = (a && e && carry3) || (a && !e && !carry3) || (!a && e && !carry3) || (!a && !e && carry3);
carry4 = (a && e && carry3) || (a && e && !carry3) || (a && !e && carry3) || (!a && e && carry3);
//Bit 1
if(l)
digitalWrite(L, HIGH);
else
digitalWrite(L,LOW);
//Bit 2
if(k)
digitalWrite(K, HIGH);
else
digitalWrite(K,LOW);
//Bit 3
if(j)
digitalWrite(J, HIGH);
else
digitalWrite(J,LOW);
//Bit 4
if(i)
digitalWrite(I, HIGH);
else
digitalWrite(I,LOW);
//Carry
if(carry4)
digitalWrite(Carry, HIGH);
else
digitalWrite(Carry,LOW);
//Delay
delay(100);
}
Its supose to show the numbers being added up (modified with slide switchs) and the result as the series of LEDs (on and off) that makes up the number, but testing the code, it just doesnt work, it only turns up the LEDs of the result (except for the carry).
Also Im asked to see if I can change the use of slide switchs for buttons.
I'm not very good at python, and I really dont want to waste anyones time, but I wanted to see if someone could help. I would really want learn how to fix these and would appreciate any help.