I am new to python but have some experience programming.
I am trying to output audio using pushbuttons with a small catch.
When two buttons are pressed almost simultaneously we must wait for the first sound to finish before playing the next sound. We are taking the time for each sound to be approximately 3 seconds.
This ideally needs to be extended to more buttons going forward
I have attached where I have got to in the code below, this code specifically has issues as the timer function cannot be used more than once.
Code: Select all
import os
from threading import Timer
from time import sleep
from sys import exit
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
button_1active = False
def sound2():
print ("playsound2")
#os.system('mpg123 -q Two.mp3 &')
# print("two")
#Timer setup
soundtime = 3
t = Timer(soundtime,sound2)
while True:
try:
if (GPIO.input(23) == False):
os.system('mpg123 -q One.mp3 &')
print("1")
button_1active =True
if (GPIO.input(24) == False):
os.system('mpg123 -q Two.mp3 &')
print("two")
if (button_1active == True):
if(GPIO.input(24) == False):
t.start()
sleep(0.5);
except KeyboardInterrupt:
exit()