Utilisation d’un buzzer

Pymata4 propose un réglage particulier pour le buzzer.

L’initialisation se fait par la méthode set_pin_mode_tone(pin).

Ensuite, deux méthodes existent pour jouer un son :

  • play_tone(pin,fréquence,durée)
  • play_tone_continuously(pin, fréquence)

Dans les exemples suivants, on utilise le module keyboard de Python pour détecter les événements clavier (touches 1 à 8 pour les notes).

Exemple d’utilisation de play_tone()

from pymata4 import pymata4
import sys
import time
import keyboard

#------------ Les notes à jouer
def on_1():
    carte.play_tone(Buzzer,261, Duree) # DO
def on_2():
    carte.play_tone(Buzzer,294, Duree) # RE
def on_3():
    carte.play_tone(Buzzer,330, Duree) # MI
def on_4():
    carte.play_tone(Buzzer,349, Duree) # FA
def on_5():
    carte.play_tone(Buzzer,392, Duree) # SOL
def on_6():
    carte.play_tone(Buzzer,440, Duree) # LA
def on_7():
    carte.play_tone(Buzzer,494, Duree) # SI
def on_8():
    carte.play_tone(Buzzer,523, Duree) # DO


carte = pymata4.Pymata4()

print("--------------- Début --------------")
# Constantes
Buzzer = 5 # port du buzzer : D8
Duree = 300

#----------------  programme principal
try :
    keyboard.add_hotkey("1",on_1)
    keyboard.add_hotkey("2",on_2)
    keyboard.add_hotkey("3",on_3)
    keyboard.add_hotkey("4",on_4)
    keyboard.add_hotkey("5",on_5)
    keyboard.add_hotkey("6",on_6)
    keyboard.add_hotkey("7",on_7)
    keyboard.add_hotkey("8",on_8)
    carte.set_pin_mode_tone(Buzzer)
    time.sleep(1)
    while True:
        time.sleep(1)
except KeyboardInterrupt : # si on tape Ctl + C
    print("--------------- Fin --------------")
    carte.shutdown()
    sys.exit(0)

Exemple d’utilisation de play_tone_continuously()

from pymata4 import pymata4
import sys
import time
import keyboard

#------------ Les notes à jouer
def on_1(data):
    carte.play_tone_continuously(Buzzer,261) # DO
def on_2(data):
    carte.play_tone_continuously(Buzzer,294) # RE
def on_3(data):
    carte.play_tone_continuously(Buzzer,330) # MI
def on_4(data):
    carte.play_tone_continuously(Buzzer,349) # FA
def on_5(data):
    carte.play_tone_continuously(Buzzer,392) # SOL
def on_6(data):
    carte.play_tone_continuously(Buzzer,440) # LA
def on_7(data):
    carte.play_tone_continuously(Buzzer,494) # SI
def on_8(data):
    carte.play_tone_continuously(Buzzer,523) # DO
#------------ Les notes à arrêter
def off(data):
    carte.play_tone_off(Buzzer)

carte = pymata4.Pymata4()

print("--------------- Début --------------")
# Constantes
Buzzer = 5 # port du buzzer : D8

#----------------  programme principal
try :
    keyboard.on_press_key("1",on_1)
    keyboard.on_press_key("2",on_2)
    keyboard.on_press_key("3",on_3)
    keyboard.on_press_key("4",on_4)
    keyboard.on_press_key("5",on_5)
    keyboard.on_press_key("6",on_6)
    keyboard.on_press_key("7",on_7)
    keyboard.on_press_key("8",on_8)
    keyboard.on_release_key("1",off)
    keyboard.on_release_key("2",off)
    keyboard.on_release_key("3",off)
    keyboard.on_release_key("4",off)
    keyboard.on_release_key("5",off)
    keyboard.on_release_key("6",off)
    keyboard.on_release_key("7",off)
    keyboard.on_release_key("8",off)    
    carte.set_pin_mode_tone(Buzzer)
    time.sleep(1)
    while True:
        time.sleep(1)
except KeyboardInterrupt :
    print("--------------- Fin --------------")
    carte.shutdown()
    sys.exit(0)
    

Retour à la rubrique