from machine import Pin, ADC
import time
# =========================================================
# 1. CONFIGURAÇÃO DO MULTIPLEXADOR (Pots P0 a P5 no C5 a C0)
# =========================================================
s0 = Pin(18, Pin.OUT)
s1 = Pin(19, Pin.OUT)
s2 = Pin(20, Pin.OUT)
s3 = Pin(21, Pin.OUT)
sig = ADC(Pin(26)) # GP26 (Pino Físico 31)
# =========================================================
# 2. CONFIGURAÇÃO DOS 8 BOTÕES (GP2 ao GP9)
# =========================================================
botoes_pinos = [2, 3, 4, 5, 6, 7, 8, 9]
botoes = [Pin(pino, Pin.IN, Pin.PULL_UP) for pino in botoes_pinos]
# =========================================================
# 3. CONFIGURAÇÃO DO ENCODER EC11 (GP10, GP11, GP12)
# =========================================================
encoder_clk = Pin(10, Pin.IN, Pin.PULL_UP)
encoder_dt = Pin(11, Pin.IN, Pin.PULL_UP)
encoder_sw = Pin(12, Pin.IN, Pin.PULL_UP)
# Variáveis do Encoder
posicao_encoder = 64 # Valor MIDI central (0 a 127)
ultimo_clk = encoder_clk.value()
ultimo_sw = encoder_sw.value()
# Histórico para filtro de mudança
ultimos_valores_pots = [-99] * 12
ultimos_estados_botoes = [False] * 8
# =========================================================
# FUNÇÕES DE LEITURA
# =========================================================
def selecionar_canal(canal):
""" Mapeia as 4 linhas de seleção S0-S3 do multiplexador """
s0.value(canal & 0x01)
s1.value((canal >> 1) & 0x01)
s2.value((canal >> 2) & 0x01)
s3.value((canal >> 3) & 0x01)
time.sleep_us(100) # Estabilização elétrica
def ler_potenciometros(quantidade=12):
valores = [0] * quantidade
# Lê os 6 primeiros potenciômetros em ordem inversa (C5 -> C0)
for i in range(6):
canal_chip = 5 - i
selecionar_canal(canal_chip)
_ = sig.read_u16() # Limpeza de leitura residual
time.sleep_us(20)
valores[i] = int((sig.read_u16() / 65535) * 127)
# Lê os canais livres (C6 a C11)
for i in range(6, quantidade):
selecionar_canal(i)
_ = sig.read_u16()
valores[i] = int((sig.read_u16() / 65535) * 127)
return valores
def ler_botoes():
""" Retorna True quando o botão é pressionado (GND) """
return [not botao.value() for botao in botoes]
# =========================================================
# LOOP PRINCIPAL
# =========================================================
print("==================================================")
print(" SISTEMA PRONTO: 8 BOTS | 6 POTS | ENCODER EC11 ")
print("==================================================")
time.sleep(0.5)
while True:
mudou_algo = False
# --- A. LEITURA DO GIRO DO ENCODER ---
estado_clk_atual = encoder_clk.value()
if estado_clk_atual != ultimo_clk and estado_clk_atual == 0:
if encoder_dt.value() != estado_clk_atual:
posicao_encoder = min(127, posicao_encoder + 1) # Giro horário
else:
posicao_encoder = max(0, posicao_encoder - 1) # Giro anti-horário
mudou_algo = True
ultimo_clk = estado_clk_atual
# --- B. LEITURA DO CLIQUE DO ENCODER (SW) ---
estado_sw_atual = not encoder_sw.value()
if estado_sw_atual != ultimo_sw:
ultimo_sw = estado_sw_atual
mudou_algo = True
# --- C. LEITURA DOS POTENCIÔMETROS E BOTÕES ---
valores_pots = ler_potenciometros(quantidade=12)
estados_botoes = ler_botoes()
log_pots = "POTS: "
log_botoes = "BOTÕES: "
# Processa os 8 Botões
for i in range(8):
if estados_botoes[i] != ultimos_estados_botoes[i]:
ultimos_estados_botoes[i] = estados_botoes[i]
mudou_algo = True
status = "[X]" if ultimos_estados_botoes[i] else "[ ]"
log_botoes += f"B{i+1}:{status} "
# Processa os Potenciômetros (filtro de ruído > 2)
for i in range(12):
if abs(valores_pots[i] - ultimos_valores_pots[i]) > 2:
ultimos_valores_pots[i] = valores_pots[i]
mudou_algo = True
log_pots += f"P{i}:{ultimos_valores_pots[i]:3} | "
# --- D. EXIBIÇÃO APENAS EM CASO DE ALTERAÇÃO ---
if mudou_algo:
status_sw = "[X]" if ultimo_sw else "[ ]"
print(f"{log_pots} || {log_botoes} B_ENC:{status_sw} || 🔄 ENC: {posicao_encoder:3}")
time.sleep(0.005) # Ciclo super veloz para não perder os passos do encoder
Loading
cd74hc4067
cd74hc4067