from machine import Pin, ADC, PWM
from time import sleep
import urandom
# Segments : a, b, c, d, e, f, g
segments = [
Pin(22, Pin.OUT), Pin(21, Pin.OUT), Pin(20, Pin.OUT),
Pin(19, Pin.OUT), Pin(18, Pin.OUT), Pin(16, Pin.OUT), Pin(17, Pin.OUT)
]
# Chiffres de 0 à 6
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
]
# Bouton sur GP15
button = Pin(10, Pin.IN, Pin.PULL_DOWN)
# Potentiomètre sur GP28
pot = ADC(28)
# PWM sur chaque segment
pwms = [PWM(seg) for seg in segments]
for pwm in pwms:
pwm.freq(1000)
# Affichage du chiffre
def show_digit(n):
for i in range(7):
pwms[i].duty_u16(65535 if digits[n][i] else 0)
# Éteindre l'affichage
def clear_display():
for pwm in pwms:
pwm.duty_u16(0)
last_state = 0
while True:
if button.value() and not last_state:
pot_value = pot.read_u16() # 0 à 65535
display_time = 1 + (pot_value / 65535) * 9 # 1 à 10 sec
number = urandom.getrandbits(3) % 7
show_digit(number)
sleep(display_time)
clear_display()
sleep(0.2) # anti-rebond
last_state = button.value()
sleep(0.01)