Came up with this code for one of three sounds to play when a motion sensor picks something up. I want the ability to pause and play the loop when a key on the keyboard is pressed. I'm really not sure how to implement this.
Liam
import time
import os
from gpiozero import MotionSensor
import random
import pygame
pygame.init()
pir = MotionSensor(4)
while True:
if pir.motion_detected:
print("Motion detected")
_songs = ('/home/pi/Documents/mp3/1.mp3','/home/pi/Documents/mp3/2.mp3','/home/pi/Documents/mp3/3.mp3')
_currently_playing_song = None
def play_a_different_song():
global _currently_playing_song, _songs
next_song = random.choice(_songs)
while next_song == _currently_playing_song:
next_song = random.choice(_songs)
_currently_playing_song = next_song
pygame.mixer.music.load(next_song)
pygame.mixer.music.play(0)
time.sleep(10)
Thanks,Liam
