Hi all,
I have a Raspberry Pi 3B+ and I use it to activate a .wav file when motion is detected. I want it to have an infinite loop, where it can run the "while True" part of the code again and again. How can I do that? Also, I am a beginner for this coding language, so can someone please check my code that it works?Thanks.
Aden
I have a Raspberry Pi 3B+ and I use it to activate a .wav file when motion is detected. I want it to have an infinite loop, where it can run the "while True" part of the code again and again. How can I do that? Also, I am a beginner for this coding language, so can someone please check my code that it works?
Code:
from gpiozero import MotionSensorimport pygameimport time#VariablesPIR_PIN = (4)#File Setuppath = "/home/pi49366/12_drum_sounds/"sound_files = ["bass_hit_c.wav", "bd_tek.wav", "two_crickets.wav"]#GPIO SETUPGPIO.setup(PIR_PIN, GPIO.IN) #pygame Setuppygame.mixer.init()speaker_volume = 0.5 #50% volumepygame.mixer.music.set_volume(speaker_volume)# Initialize Pygame pygame.mixer.init()def play_sound(): pygame.mixer.music.load(path + sound_files) # Change to your .wav file path pygame.mixer.music.play()def is_within_time_range(start_time, end_time): current_time = datetime.now().time() return start_time <- current_time <= end_time#Defines time rangestart_time = datetime.strptime("16:30", "%H:%M").time()end_time = datetime.strptime("06:30", "%H:%M").time()if time == end_time: print("Total Hours On: 14 Hours 00 Minutes, 840 Minutes") print("Waiting For Motion...")while True: if GPIO.setup(PIR_PIN): print("Motion Detected, playing sound!") play_sound() time.sleep(0.1) #Small delay to avoid excessive CPU useage finally: GPIO.cleanup() #Clean up GPIO on exit
Aden
Statistics: Posted by aden77 — Wed Nov 27, 2024 1:39 am