Stap 3: Software-instellingen
Ik voegde 5 mp3 liederen en ze speelden op een willekeurige volgorde.
#!/usr/bin/env python from time import sleep import os import RPi.GPIO as GPIO import time import subprocess from random import randint GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) GPIO.setup(17, GPIO.OUT) GPIO.setup(18, GPIO.OUT) GPIO.setup(21, GPIO.OUT) GPIO.setup(22, GPIO.OUT) init = True def all(): GPIO.output(18, True) GPIO.output(21, True) GPIO.output(22, True) sleep(.3) GPIO.output(18, False) GPIO.output(21, False) GPIO.output(22, False) def one(): GPIO.output(18, True) sleep(.3) GPIO.output(18, False) def two(): GPIO.output(21, True) sleep(.3) GPIO.output(21, False) def three(): GPIO.output(22, True) sleep(.3) GPIO.output(22, False) while True: if ( GPIO.input(23) == True ): print "Motion Detected" if (init == True): print "Initializing for 10 seconds" init =False sleep(10) i = randint(1,5) song = "/home/pi/" + str (i) + ".mp3" reccmd = ["/usr/bin/mpg321", "-q", song] p = subprocess.Popen(reccmd, stdout=subprocess.PIPE) GPIO.output(17, True) while (p.poll() == None): all() one() two() three() all() three() two() one() all() one() two() three() all() three() two() one() one() two() three() one() two() three() one() two() three() one() two() three() GPIO.output(17, False) sleep(5); sleep(30);