The script starts by declaring a list named digits with single character strings '0' and '1'.
digits = ['0','1']
The display.show(Image.ARROW_W) call makes the LED display point at the A button as a prompt to press it to start the process. After that, the while True: loop repeatedly checks if the A button was pressed, and does nothing else until you press it. After the A button is pressed, the display.clear() call erases the arrow so that it can start displaying the 0/1 digits without any leftover LED pixels from the arrow.
display.show(Image.ARROW_W) while True: if button_a.was_pressed(): display.clear()
These three nested loops cycle through each possible combination in pin. The script starts setting a to '0', then b to '0', and c to '0'. Then, pin = ''.join([a, b, c]) creates the string '000' by combining the three characters. The for c in digits loop isn’t done yet so it sets c to '1'. Then, pin = ''.join([a, b, c]) repeats, and this time, the result is '001'.
for a in digits: for b in digits: for c in digits: pin = ''.join([a, b, c])
Now that the for c in digits loop is finished, the for b in digits loop has to do its next iteration, setting b to '1'. Since for c in digits is below and indented, it has to repeat that loop again. This time, a is still '0', but b is now '1'. So, when c is '0', the result of pin = ''.join([a, b, c]) is '010'. On the for c in digits second repetition, it sets c to '1' and the result of pin = ''.join([a, b, c]) is '011'. It continues this way through all the possible 0/1 combinations.
A lot more happens each time through the for c loop. First, it displays the current pin combination in the terminal.
print("pin =", pin)
Then, it displays the PIN as 0/1 digits with the LED display.
for x in range(0, len(pin)): bit = int(pin[x]) brightness = bit * 9 display.set_pixel(x,4,9) for y in range(0, 4): display.set_pixel(x, y, brightness)
It also checks for the radio response from the Vault Receiver micro:bit. This loop sends repeatedly and checks for a response each time. When it does get a response, it exits the while response is None:… loop.
response = None while response is None: radio.send(pin) sleep(100) response = radio.receive()
Still inside the for c in digits… loop. Once a response has been received, it prints to the terminal. Then, if the response is "Access granted.", it means the PIN was correct. At that point, the correct PIN scrolls through the PIN Pad Transmitter micro:bit’s display repeatedly.
print(response) if response == "Access granted.": while True: display.scroll(pin)
The response string could also be "Access denied." In that case, the script just waits 4 seconds, then clears its display and lets for c in digits…loop to repeat with the next pin string in the sequence.
sleep(4000) display.clear()