# Nama : Yuningsih Vebhy Selvania
# NIM : 2210953042
# Kelompok 9
# Modul 1 Percobaan 7 Kondisi 1
# Buatlah rangkaian seperti gambar pada percobaan 7 dan output akhir rangkaian keseluruhannya ditunjukkan dengan LED RGB menampilkan warna sesuai dengan input push button tetapi saat push button LED RGB warna merah ditekan, Buzzer berbunyi dan push button warna lainnya Buzzer mati
from machine import Pin
import utime
# Konfigurasi pin
LED_RED = Pin(5, Pin.OUT)
LED_GREEN = Pin(6, Pin.OUT)
LED_BLUE = Pin(11, Pin.OUT)
BUZZER = Pin(12, Pin.OUT)
BTN_RED = Pin(10, Pin.IN, Pin.PULL_UP)
BTN_GREEN = Pin(7, Pin.IN, Pin.PULL_UP)
BTN_BLUE = Pin(8, Pin.IN, Pin.PULL_UP)
# Variabel status LED (awalannya mati)
redState = False
greenState = False
blueState = False
# Variabel status tombol sebelumnya
lastRedBtnState = True
lastGreenBtnState = True
lastBlueBtnState = True
def updateLEDs():
LED_RED.value(redState) # LED merah
LED_GREEN.value(greenState) # LED hijau
LED_BLUE.value(blueState) # LED biru
def buzz(duration):
BUZZER.value(1) # Nyalakan buzzer
utime.sleep_ms(duration)
BUZZER.value(0) # Matikan buzzer
while True:
# Membaca status tombol
redBtnState = BTN_RED.value()
greenBtnState = BTN_GREEN.value()
blueBtnState = BTN_BLUE.value()
# Tombol merah ditekan
if redBtnState == 0 and lastRedBtnState == 1:
redState = True # Nyalakan LED merah
greenState = False # Matikan LED hijau
blueState = False # Matikan LED biru
print("Tombol Merah Ditekan")
buzz(100) # Bunyi buzzer
# Tombol hijau ditekan
elif greenBtnState == 0 and lastGreenBtnState == 1:
redState = False # Matikan LED merah
greenState = True # Nyalakan LED hijau
blueState = False # Matikan LED biru
print("Tombol Hijau Ditekan")
# Tombol biru ditekan
elif blueBtnState == 0 and lastBlueBtnState == 1:
redState = False # Matikan LED merah
greenState = False # Matikan LED hijau
blueState = True # Nyalakan LED biru
print("Tombol Biru Ditekan")
# Perbarui LED sesuai status tombol yang ditekan
updateLEDs()
# Update status tombol sebelumnya untuk deteksi perubahan status tombol
lastRedBtnState = redBtnState
lastGreenBtnState = greenBtnState
lastBlueBtnState = blueBtnState
# Debounce
utime.sleep_ms(50)
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
bz1:1
bz1:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2