【发布时间】:2019-11-18 02:53:52
【问题描述】:
我正在构建一组带有运动传感器和树莓派的音乐楼梯,我对 python 还是很陌生。目前,当用户通过运动传感器时,会在 .wav 文件的整个持续时间内播放一个音符,但我想知道有没有一种方法可以让声音只在运动传感器通过的时间内播放?
import RPi.GPIO as GPIO
import pygame.mixer
pygame.mixer.init()
'''GPIO setup'''
GPIO.setmode(GPIO.BCM)
GPIO.setwarningS(False)
'''Define stairs and GPIO pins'''
step1 = 4
step2 = 17
step3 = 27
'''Motion sensor setup'''
GPIO.setup(step1, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(step2, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(step3, GPIO.IN, GPIO.PUD_UP)
'''Sound files'''
C1 = pygame.mixer.Sound("piano/C1.wav")
D = pygame.mixer.Sound("piano/D.wav")
E = pygame.mixer.Sound("piano/E.wav")
def play(pin):
sound = sound_pins[pin]
sound.play()
'''Dictionary of steps and sounds'''
sound_pins = {
step1: C1,
step2: D,
step3: E,
}
for pin in sound_pins:
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
GPIO.add_even_detect(pin, GPIO.RISING, play, 100)
【问题讨论】:
标签: python-2.7 pygame raspberry-pi3