import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico!")
from machine import Pin, ADC
switch = Pin(15, machine.Pin.IN)
pot = ADC(26)
led = Pin(14, machine.Pin.OUT)
# Segment pins (catodo comune → HIGH = ON)
seg_pins = [
machine.Pin(2, machine.Pin.OUT),
machine.Pin(3, machine.Pin.OUT),
machine.Pin(4, machine.Pin.OUT),
machine.Pin(5, machine.Pin.OUT),
machine.Pin(6, machine.Pin.OUT),
machine.Pin(7, machine.Pin.OUT),
machine.Pin(8, machine.Pin.OUT)
]
# Codifica cifre 0–9 (catodo comune)
digits = {
0: [1,1,1,1,1,1,0],
1: [0,1,1,0,0,0,0],
2: [1,1,0,1,1,0,1],
3: [1,1,1,1,0,0,1],
4: [0,1,1,0,0,1,1],
5: [1,0,1,1,0,1,1],
6: [1,0,1,1,1,1,1],
7: [1,1,1,0,0,0,0],
8: [1,1,1,1,1,1,1],
9: [1,1,1,1,0,1,1]
}
def cancella():
for p in seg_pins:
p.value(0)
def mostra_cifra(n):
pattern = digits[n]
for pin, val in zip(seg_pins, pattern):
pin.value(val)
# --- Main loop ---
while True:
if switch.value() == 0:
# Interruttore OFF
cancella()
led.value(1)
time.sleep(0.1)
continue
# Interruttore ON
led.value(0)
lettura = pot.read_u16()
percento = int((lettura / 65535) * 100)
# Cifra delle decine
if percento == 100:
digit = 0
else:
digit = percento // 10
mostra_cifra(digit)
time.sleep(0.1)