Created
May 20, 2019 03:39
-
-
Save idriszmy/533ddde240f5bb99c8f508b978289aca to your computer and use it in GitHub Desktop.
Menghasilkan bunyi daripada pembaz (Contoh 2).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Fungsi kod: Menghasilkan bunyi daripada pembaz (Contoh 2). | |
# Penulis: Idris Zainal Abidin | |
# Blog: https://idrisz.my | |
# Kemaskini: 20 Mei 2019 | |
# | |
from gpiozero import Button, Buzzer # Import fungsi Button dan Buzzer | |
# daripada pustaka gpiozero | |
from time import sleep # Import fungsi sleep dari pustaka time | |
sw1 = Button(21) # sw1 bersambung pada GPIO21 | |
sw2 = Button(16) # sw1 bersambung pada GPIO16 | |
sw3 = Button(20) # sw1 bersambung pada GPIO20 | |
buzzer = Buzzer(26) # Pembaz disambungkan pada GPIO26 | |
print("Jom Belajar Bersama Idris di idrisz.my") # Paparkan mesej di Python Shell | |
# Fungsi beep memerlukan 3 pembolehubah | |
# Pembolehubah 1: Tempoh isyarat HIGH (bunyi) dalam saat | |
# Pembolehubah 2: Tempoh isyarat LOW (senyap) dalam saat | |
# Pembolehubah 3: Berapa kali ulang? | |
buzzer.beep(0.1, 0.1, 2) | |
try: | |
while True: # Pernyataan berulang utama | |
if sw1.is_pressed == True: # Jika sw1 ditekan | |
print("Butang sw1 ditekan.") # Paparkan mesej | |
buzzer.beep(0.1, 0.1, 1) # Bunyi bip sekali | |
elif sw2.is_pressed == True: # Jika sw2 ditekan | |
print("Butang sw2 ditekan.") # Paparkan mesej | |
buzzer.beep(0.1, 0.1, 2) # Bunyi bip 2 kali | |
elif sw3.is_pressed == True: # Jika sw3 ditekan | |
print("Butang sw3 ditekan.") # Paparkan mesej | |
buzzer.beep(0.1, 0.1, 3) # Bunyi bip 3 kali | |
except KeyboardInterrupt: | |
buzzer.off() # Pembaz senyap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment