How the Decimal Code Works

Node How it Works: decimal_pin_pad_transmitter

Compared to the first binary pin_pad_transmitter script, this decimal version has the same statements, up through x = len(pin).

# decimal_pin_pad_transmitter.py

from microbit import *
import radio

radio.on()
radio.config(channel=7)

pin = ''
n = 0

while True:

    x = len(pin)

 

When button A is pressed to select the next digit, the script remembers that digit by increasing the value of n.  It also represents 0 with no lights, 1 with the row 0 LED on, 2 with the row 1 LED on, and so on, up through 5 with the row 4 LED on.  The if n < 5 block takes care of adding 1 to n and successively turning on each LED in the column.  If all the lights are on and you press A again, the else block takes care of setting n to 0 and turning off all the lights.

    if button_a.was_pressed():
        if n < 5:
            n += 1
            if n is not 0:
                y = n - 1
                display.set_pixel(x, y, 9)
        else:
            for y in range(0, 5):
                display.set_pixel(x, y, 0)
            n = 0

 

Take a close look at if n is not 0 above.  It prevents any lights from turning on when n is 0.  When n is 1, it has to turn on the light in row 0.  Since the row with the light on has an index of one below the value of n, y = n – 1 stores the value of the correct row index in y to turn on the correct LED.  

When button B is pressed, the script has to update the pin string and reset n to 0 so that you can enter the next column.  If button B is pressed to enter a digit in the third column, it also has to send the pin string to the decimal vault micro:bit.  

    if button_b.was_pressed():
        pin += str(n)
        n = 0
        if len(pin) == 3:
            radio.send(pin)
            display.scroll(pin)
            pin = ''
            display.clear()

 

In pin += str(n), the str(n) call returns a string with a single digit character that represents the value n stores.  That character could be '0', '1', '2', '3', '4', or '5'.  The pin += part appends that character to the pin string.  When the pin string is appended with the third digit, len(pin) will return 3.  At that point, the if len(pin) == 3 block sends the pin string to the Vault Receiver micro:bit, scrolls the digits across the LED display, resets pin to an empty string, and clears the display.  

How it Works: decimal_bank_vault_receiver

Aside from the comment with the script’s name, only three other lines were changed.  In the original binary version, the script initialized pin to '011', then displayed a dot in the center of the LED display.

pin = '011'

while True:

    display.set_pixel(2,2,9)

 

In the decimal version, all that really needed to change was the pin string—from '011' to '324'.  Since there might be some confusion about whether the original binary or updated decimal version of the vault receiver script is running, the single pixel in the center of the binary version was changed to a small square in the center of the decimal version.

pin = '324'

while True:

    display.show(Image.SQUARE_SMALL)