Last active
May 20, 2019 06:37
-
-
Save idriszmy/a46684ac0a43e10711e7c2ec4cbe400f to your computer and use it in GitHub Desktop.
Menghasilkan bunyi daripada pembaz (Contoh 3).
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 3). | |
# Penulis: Idris Zainal Abidin | |
# Blog: https://idrisz.my | |
# Kemaskini: 20 Mei 2019 | |
# | |
from gpiozero import LED, Button, Buzzer # Import fungsi LED, Button dan Buzzer | |
# daripada pustaka gpiozero | |
from time import sleep # Import fungsi sleep dari pustaka time | |
from os import system # Import system dari pustaka os | |
led1 = LED(17) # led1 bersambung pada GPIO17 | |
led8 = LED(19) # led8 bersambung pada GPIO19 | |
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 | |
led1.on() # led1 menyala | |
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 | |
led8.on() # led8 menyala | |
buzzer.beep(0.1, 0.1, 1) # Bunyi bip sekali | |
elif sw2.is_pressed == True: # Jika sw2 ditekan | |
print("Butang sw2 ditekan.") # Paparkan mesej | |
led8.off() # led8 menyala | |
buzzer.beep(0.1, 0.1, 1) # Bunyi bip sekali | |
elif sw3.is_pressed == True: # Jika sw3 ditekan | |
print("Butang sw3 ditekan.") # Paparkan mesej | |
led1.off() # led1 padam | |
buzzer.beep(0.2, 0.2, 3) # Bunyi bip 3 kali | |
sleep(1) # Tunggu 1 saat | |
system('sudo shutdown -h now') # Mematikan Raspberry Pi | |
except KeyboardInterrupt: | |
led1.off() #led7 padam | |
led8.off() # led8 padam | |
buzzer.off() # Pembaz senyap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment