# Import modul yang dibutuhkan
from machine import Pin
import time
import neopixel # Library Neopixel yang sudah diunggah
# --- Konfigurasi Neopixel ---
NEOPIXEL_PIN = 14 # Pin GPIO terhubung ke Data In (DIN) Neopixel
NUM_PIXELS = 16 # Jumlah LED di strip/ring Neopixel Anda
# Inisialisasi objek Neopixel
np = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NUM_PIXELS)
# --- Fungsi Bantu untuk Warna Pelangi (wheel) ---
# Mengonversi posisi roda warna (0-255) ke nilai RGB
def wheel(pos):
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
# --- Fungsi Animasi ---
# Efek: Mengisi strip dengan satu warna
def color_fill(color, delay=0):
print(f"Mengisi warna {color}...")
for i in range(NUM_PIXELS):
np[i] = color
np.write()
if delay > 0:
time.sleep_ms(delay)
# Efek: Color Wipe - piksel menyala satu per satu
def color_wipe(color, delay_ms=10):
print(f"Menjalankan Color Wipe dengan warna {color}...")
for i in range(NUM_PIXELS):
np[i] = color
np.write()
time.sleep_ms(delay_ms)
time.sleep(0.5) # Jeda setelah efek selesai
# Efek: Rainbow Cycle - siklus pelangi di seluruh strip
def rainbow_cycle(delay_ms=20, iterations=1):
print("Menjalankan Rainbow Cycle...")
for j in range(256 * iterations): # Ulangi siklus beberapa kali
for i in range(NUM_PIXELS):
pixel_index = (i * 256 // NUM_PIXELS) + j # Pergeseran warna untuk setiap piksel
np[i] = wheel(pixel_index & 255) # '& 255' untuk memastikan nilai di 0-255
np.write()
time.sleep_ms(delay_ms)
time.sleep(0.5)
# Efek: Blink semua piksel dengan warna tertentu
def blink_all_pixels(color, blink_count=3, delay_on_ms=200, delay_off_ms=200):
print(f"Mengedipkan warna {color} {blink_count} kali...")
original_colors = [np[i] for i in range(NUM_PIXELS)] # Simpan warna asli
for _ in range(blink_count):
set_all_pixels(color)
time.sleep_ms(delay_on_ms)
set_all_pixels((0, 0, 0)) # Mati
time.sleep_ms(delay_off_ms)
# Kembalikan ke warna asli setelah blink (opsional)
for i in range(NUM_PIXELS):
np[i] = original_colors[i]
np.write()
time.sleep(0.5)
print("Memulai program Efek Animasi Neopixel...")
# Loop utama untuk menjalankan berbagai animasi
while True:
# Mematikan semua LED di awal setiap siklus
set_all_pixels((0, 0, 0))
time.sleep(0.5)
color_wipe((0, 0, 255)) # Biru
color_wipe((255, 0, 0)) # Merah
rainbow_cycle(delay_ms=10, iterations=3) # Pelangi lebih cepat, 3 iterasi
blink_all_pixels((0, 255, 0), blink_count=5, delay_on_ms=100, delay_off_ms=100) # Kedip hijau cepat
# Contoh kombinasi: nyalakan putih, lalu padam perlahan
print("Menyalakan putih lalu padam perlahan...")
set_all_pixels((255, 255, 255))
for brightness in range(255, -1, -5):
for i in range(NUM_PIXELS):
r, g, b = np[i]
# Sesuaikan kecerahan dengan mempertahankan rasio warna
np[i] = (int(r * brightness / 255), int(g * brightness / 255), int(b * brightness / 255))
np.write()
time.sleep_ms(20) # Jeda untuk efek memudar
time.sleep(1) # Jeda sebelum siklus berikutnya