import machine
from utime import sleep
s0 = machine.Pin(27, machine.Pin.OUT)
s1 = machine.Pin(28, machine.Pin.OUT)
mux_in = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)
prev_binary_code = 0
while True:
binary_code = 0
for selector_val in range(4):
# | S1 | S0 | D |
# |----|----|-----|
# | 0 | 0 | 0 |
# | 0 | 1 | 1 |
# | 1 | 0 | 2 |
# | 1 | 1 | 3 |
#
# from the mux truth table that shows
# which Data Input is selected based on the select values
# we can deduce how to formulate S0 and S1
# s0: 0 - 1 - 0 - 1, remainder of the select-count from /2
# the modulo operation
# 0 % 2 = 0
# 1 % 2 = 1
# 2 % 2 = 0
# 3 % 2 = 1
s0.value(selector_val % 2)
# s1: 0 - 0 - 1 - 1, integer part of select-count /2
# 0 // 2 = 0
# 1 // 2 = 0
# 2 // 2 = 1
# 3 // 2 = 1
s1.value(selector_val // 2)
sleep(0.020)
# (10^1 * digit_1) + (10^0 * digit_0)
# example: 28 -> (10^1 * 2) + (10^0 * 8)
# (2^n * input_n) + ... + (2^1 * input_1) + (2^0 * input_0)
binary_code += (pow(2, selector_val) * mux_in.value())
if binary_code != prev_binary_code:
print(f'decimal representation: {binary_code}')
prev_binary_code = binary_code
sleep(0.100)