You are to simulate a 2-bit half adder. You are not required to implement a real 2-bit half adder using logic gates; you are going to implement a black-box system using the microcontroller that functions as a 2-bit half adder.
Your system will have 4 inputs (2 for the first 2-bit number, and another 2 for the second 2-bit number), and 2 outputs (2-bit output for the sum).
Inputs: Each input is controlled via a separate push-down button. The inputs should be toggled each time the buttons are released,as opposed to being LOW while the button is released, and HIGH while the button is pushed. The initial values of inputs will be LOW.
Outputs: Each output is controlled via a separate LED. The LED will be off while the output is LOW, and on while the output is HIGH.
Since the states of the inputs will be invisible to the naked eye, you are to represent the input states with additional LEDs as well.
byte numberOneFirstDigit = 3;
byte numberOneSecondDigit = 2;
byte numberTwoFirstDigit = 5;
byte numberTwoSecondDigit = 4;
byte sumFirstDigit=7;
byte sumSecondDigit=6;
byte carryDigit=12;
byte numberOneFirstDigitButton = 9;
byte numberOneSecondDigitButton = 8;
byte numberTwoFirstDigitButton = 11;
byte numberTwoSecondDigitButton= 10;
void setup()
{
pinMode(numberOneFirstDigit,OUTPUT);
pinMode(numberOneSecondDigit,OUTPUT);
pinMode(numberTwoFirstDigit,OUTPUT);
pinMode(numberTwoSecondDigit,OUTPUT);
pinMode(sumFirstDigit,OUTPUT);
pinMode(sumSecondDigit,OUTPUT);
pinMode(carryDigit,OUTPUT);
pinMode(numberOneFirstDigitButton,INPUT);
pinMode(numberOneSecondDigitButton,INPUT);
pinMode(numberTwoFirstDigitButton,INPUT);
pinMode(numberTwoSecondDigitButton,INPUT);
}
void loop()
{
byte numberOneFirstDigit_state = digitalRead(numberOneFirstDigitButton);
byte numberOneSecondDigit_state = digitalRead(numberOneSecondDigitButton);
byte numberTwoFirstDigit_state = digitalRead(numberTwoFirstDigitButton);
byte numberTwoSecondDigit_state = digitalRead(numberTwoSecondDigitButton);
if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == LOW && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , LOW);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , HIGH);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , HIGH);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == LOW && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , LOW);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , HIGH);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , LOW);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == LOW && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , LOW);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , HIGH);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == LOW)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , LOW);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , HIGH);
digitalWrite(sumSecondDigit , LOW);
digitalWrite(carryDigit , HIGH);
}
else if(numberOneSecondDigit_state == HIGH && numberOneFirstDigit_state == HIGH && numberTwoSecondDigit_state == HIGH && numberTwoFirstDigit_state == HIGH)
{
digitalWrite(numberOneFirstDigit , HIGH);
digitalWrite(numberOneSecondDigit , HIGH);
digitalWrite(numberTwoFirstDigit , HIGH);
digitalWrite(numberTwoSecondDigit , HIGH);
digitalWrite(sumFirstDigit , LOW);
digitalWrite(sumSecondDigit , HIGH);
digitalWrite(carryDigit , HIGH);
}
}
I have showed the question and the circuit I did.
I am open to any suggestions to improve my code (and maybe the circuit).
I want a program which is not too many lines.