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 (pleine luminosité)
def show_digit(n):
for i in range(7):
pwms[i].duty_u16(65535 if digits[n][i] else 0)
# Éteindre tous les segments
def clear_display():
for pwm in pwms:
pwm.duty_u16(0)
last_state = 0
while True:
if button.value() and not last_state:
# Lire la durée d'affichage depuis le potentiomètre
pot_value = pot.read_u16() # 0 à 65535
display_time = 1 + (pot_value / 65535) * 9 # 1 à 10 secondes
# Générer un chiffre aléatoire entre 0 et 6
number = urandom.getrandbits(3) % 7
# Afficher le chiffre
show_digit(number)
# Attendre pendant la durée définie
sleep(display_time)
# Effacer l'affichage
clear_display()
# Anti-rebond
sleep(0.2)
last_state = button.value()
sleep(0.01)