from machine import Pin, PWM, ADC
from time import sleep
import utime
# Inisialisasi
pot = ADC(26) # GP26 = ADC0
servo = PWM(Pin(16))
buzzer = PWM(Pin(14))
# Konfigurasi PWM
servo.freq(50) # 50 Hz untuk servo
buzzer.freq(1000) # Awal frekuensi buzzer
# Fungsi map
def map_value(value, in_min, in_max, out_min, out_max):
return int((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
# Posisi servo awal (mulai dari tengah)
current_angle = 0
while True:
pot_value = pot.read_u16() # Nilai ADC 16-bit (0 - 65535)
# Buat batas bawah dan atas supaya lebih stabil
if pot_value > 35000: # Misal potensiometer ke kiri → kecilkan sudut
current_angle += 1 # Mundur searah jarum jam
if current_angle < 0:
current_angle = 0
elif pot_value < 30000: # Jika pot ke kanan (besar) → servo diam
pass # Tidak melakukan perubahan sudut
# Konversi sudut ke duty cycle (1638 - 8192)
duty = map_value(current_angle, 0, 180, 1638, 8192)
servo.duty_u16(duty)
# === Buzzer ===
if 0 < current_angle < 180:
buzzer.duty_u16(1000) # Nyalain buzzer
else:
buzzer.duty_u16(0) # Matikan buzzer
# Debug
print(f"Pot Value: {pot_value}, Angle: {current_angle}")
sleep(0.05)