import machine
import time
import _thread
potentiometer = machine.ADC(machine.Pin(34))
button = machine.Pin(12, machine.Pin.IN)
led1 = machine.Pin(14, machine.Pin.OUT)
led2 = machine.Pin(15, machine.Pin.OUT)
led3 = machine.Pin(13, machine.Pin.OUT)
led_pwm2 = machine.PWM(led2)
def brightness_control_task():
while True:
potentiometer_value = potentiometer.read()
led_brightness = int((potentiometer_value / 4095) * 1023)
led_pwm2.duty(led_brightness)
print("Nilai Potensiometer:", potentiometer_value)
time.sleep(0.1)
def button_control_task():
prev_button_state = 0
while True:
button_state = button.value()
if button_state == 0 and prev_button_state == 1:
led1.value(0)
print("Tombol dilepas - LED 1 OFF")
time.sleep(0.1)
elif button_state == 1 and prev_button_state == 0:
led1.value(1)
print("Tombol ditekan - LED 1 ON")
prev_button_state = button_state
time.sleep(0.1)
def blink_led3_task():
while True:
led3.value(not led3.value())
time.sleep(5)
_thread.start_new_thread(brightness_control_task, ())
_thread.start_new_thread(button_control_task, ())
_thread.start_new_thread(blink_led3_task, ())