from machine import Pin import time import random _init() button = Pin(0, Pin.IN) state_old = 1 segments = [14, 13, 4, 5, 12, 16, 15] # ABCDEFG for s in segments: led = Pin(s, Pin.OUT).off() digits = [ [1, 1, 1, 1, 1, 1, 0], # 0 [0, 1, 1, 0, 0, 0, 0], # 1 [1, 1, 0, 1, 1, 0, 1], # 2 [1, 1, 1, 1, 0, 0, 1], # 3 [0, 1, 1, 0, 0, 1, 1], # 4 [1, 0, 1, 1, 0, 1, 1], # 5 [1, 0, 1, 1, 1, 1, 1], # 6 [1, 1, 1, 0, 0, 0, 0], # 7 [1, 1, 1, 1, 1, 1, 1], # 8 [1, 1, 1, 1, 0, 1, 1], # 9 ] def bit_summ(data): count = 0 while data: count += data & 1 data >>= 1 return count def draw_digit(digit): for el in enumerate(digits[digit]): if (el[1]): Pin(segments[el[0]]).on() else: Pin(segments[el[0]]).off() while True: new_state = button.value() if new_state == 0 and state_old == 1: rand_bits = random.getrandbits(5) rand_digit = bit_summ(rand_bits) rand_digit+=1 draw_digit(rand_digit) state_old = new_state