from machine import Pin
from time import sleep
# Inputs
# A = Door sensor
# B = Window sensor
# C = Security system armed
A_btn = Pin(18, Pin.IN, Pin.PULL_UP)
B_btn = Pin(19, Pin.IN, Pin.PULL_UP)
C_btn = Pin(21, Pin.IN, Pin.PULL_UP)
# Outputs
or_led = Pin(4, Pin.OUT) # Shows A OR B
alarm_led = Pin(2, Pin.OUT) # Shows final output Y
last_state = None
print("======================================")
print(" Combinational Circuit Design Lab")
print(" Alarm = Armed AND (Door OR Window)")
print(" Boolean Expression: Y = C AND (A OR B)")
print("======================================")
print()
print("A = Door sensor")
print("B = Window sensor")
print("C = System armed")
print("Y = Alarm")
print()
print("Press the buttons and observe the LEDs.")
print("Yellow LED = A OR B")
print("Red LED = Final Alarm Output Y")
print()
print("C A B | A_OR_B | Y")
print("-------------------")
while True:
# Because buttons use PULL_UP:
# Not pressed = 1 electrically, so we invert it.
# Pressed = logical 1
A = 1 if A_btn.value() == 0 else 0
B = 1 if B_btn.value() == 0 else 0
C = 1 if C_btn.value() == 0 else 0
# Step 4 and 5:
# Boolean Expression:
# Y = C AND (A OR B)
A_OR_B = A or B
Y = C and A_OR_B
or_led.value(A_OR_B)
alarm_led.value(Y)
current_state = (C, A, B, A_OR_B, Y)
if current_state != last_state:
print(C, A, B, "| ", int(A_OR_B), " |", int(Y))
last_state = current_state
sleep(0.1)