from machine import Pin, ADC, PWM, Timer
from time import sleep
led = PWM(Pin(32), freq=1000)
button = Pin(19, Pin.IN, Pin.PULL_UP)
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
timer = Timer(0)
# Status sistem (ON/OFF)
system_on = False
# Fungsi Debounce teknik untuk menghindari pembacaan ganda pada tombol akibat getaran mekanik saat ditekan.
def debounce(t):
global system_on
if button.value() == 0:
system_on = not system_on
if not system_on:
led.duty(0) # matikan LED
print("System:", "ON" if system_on else "OFF")
def button_pressed(pin):
timer.init(mode=Timer.ONE_SHOT, period=50, callback=debounce)
# interrupt saat tombol ditekan
button.irq(trigger=Pin.IRQ_FALLING, handler=button_pressed)
while True:
if system_on:
adc_value = pot.read()
duty = int(adc_value / 4095 * 1023)
led.duty(duty)
print("ADC:", adc_value, "| PWM:", duty)
sleep(0.1)