import time
time.sleep(0.1) # Wait for USB to become ready
from arrays import OutArray
from machine import Pin
# pins for values A & V
B_list = [2, 3, 4, 5]
A_list = [6, 7, 8, 9]
# create carry in input
btn = Pin(10, Pin.IN, Pin.PULL_UP)
cin = Pin(11, Pin.OUT, value=0)
cin.value(btn.value())
# create output arrays of GPIO pins
A = OutArray(A_list)
B = OutArray(B_list)
print("Welcome to 4 bit Adder")
while True:
try:
# get values of A & B
x = input('Value of A: ')
numA = int(x) # convert to integer
while numA > 15: # check a valid value
print("Error: A must be a value less than 16")
x = input('Value of A: ')
numA = int(x)
x = input('Value of B: ')
numB = int(x) # convert to integer
while numB > 15: # check a valid value
print("Error: B must be a value less than 16")
x = input('Value of B: ')
numB = int(x)
cin.value(btn.value())
if numA < 16 and numB < 16:
A.write(numA)
B.write(numB)
print(f"A + B + Cin: {numA} + {numB} + {btn.value()} = {numA + numB + btn.value()}")
elif A > 15:
print("Error: A must be a value less than 16")
else:
print("Error: B must be a value less than 16")
time.sleep(0.1)
except KeyboardInterrupt:
break
# clear all leds at end
A.write(0)
B.write(0)
cin.write(0)
print(f'\nfinished')
CarryIn
Answer
Blue LED is overflow
greater than 4 bits