import time
from machine import Pin
time.sleep(0.1) # Wait for USB to become ready
# initialize the counter to 0
counter = 0
# initialize the input button
btn1 = Pin(28, Pin.IN, Pin.PULL_DOWN)
# initialize the output leds as per the array of pins assigned
led_pins = [21, 20, 19, 18]
# using for loop comprehension to create a list of output objects
leds = [Pin(i, Pin.OUT) for i in led_pins]
# the forever loop
while True:
if btn1.value():
counter += 1
# reset counter if reached max value of 4 digit binary number
if counter >= 16:
counter = 0
# convert the number to a string which represents its binary form
binary_value = bin(counter)[2:]
# padding with leading zeros to ensure the string is 4 char long
binary_value = "0"*(4-len(binary_value)) + binary_value
for i, state in enumerate(binary_value):
print(binary_value)
s = int(state)
leds[i].value(s)
# to prevent unstable rapid input registers
time.sleep(0.25)