samedi 25 juillet 2020

Raspberry Pi update new random list "on the fly" wav files

I have a program that is working great. Basically I'm having some fun and wrote a program to mess with my roommates. Anytime someone walks down the hall the motion detector plays a random sound from a list of funny movie clips and whatnot. It's basically a proof of concept with an older Pi. I have since purchased a newer raspberry pi with 7" monitor that I would like to put in the hall and have a program that can record sounds through a mic and add them to the list (by pushing a button on the screen). Though the program below works great, anytime I upload a new sound clip while the program is running it crashes. I forsee this being an issue, as recording any new sounds via pushing a button on the 7" touchscreen will cause the same thing to happen. LONG STORY SHORT: can this code below be modified so that adding new sounds while the program is running wont cause it to crash? OR, is there another workaround that my super rookie coding brain hasn't thought of? Much thanks to the internet community, after being a web designer for years I'm finally finding something in coding that interests me.

import pygame.mixer
from tkinter import *
import glob
import random
import time
from gpiozero import LED
from gpiozero import MotionSensor

green_led = LED(17)
pir = MotionSensor(4)
sound_list = glob.glob("sounds/*.wav")


green_led.off()
pygame.mixer.init()


while True:
   
    pir.wait_for_motion()
    print("Motion Detected")
    green_led.on()    


    pygame.mixer.Sound(random.choice(sound_list)).play()
    print("Playing Sound")

    pir.wait_for_no_motion()
    green_led.off()
    print("No Motion")



Aucun commentaire:

Enregistrer un commentaire