Problem with "Import Random" and random MP3 selection - Raspberry Pi Forums


hi all,

have slight coding issue can't seem figure out , hoping can help.

basically, have pressure pad when triggered, sends closed contact gpio 26. @ point, script {should!} randomly select mp3 (5 second audio clip), play , after bit of 'sleep', await pressure pad input , select random mp3 , on again.

happening script runs, pressure pad triggered , random mp3 selected. after mp3 plays , pad trigger again, script plays same mp3 again, not selecting random mp3 second/third/twentieth time pressure pad triggered.

if close , re run script, random mp3 selected , same mp3 loop aforementioned.

grateful input!
thanks
duncan

code: select all

#!/usr/bin/env python   import os time import sleep import rpi.gpio gpio import random  gpio.setmode(gpio.bcm) gpio.setup(26, gpio.in, pull_up_down=gpio.pud_down)  randommp3=random.choice(os.listdir("/home/pi/audio/random/"))  while true:     if (gpio.input(26)== true):         print 'pressure pad detected'         os.system('omxplayer ' + randommp3)         print randommp3         sleep(5); 

you assigning value randommp3 outside loop never gets value. try moving assignment start of if block

code: select all

#!/usr/bin/env python   import os time import sleep import rpi.gpio gpio import random  gpio.setmode(gpio.bcm) gpio.setup(26, gpio.in, pull_up_down=gpio.pud_down)  while true:     if (gpio.input(26)== true):         print 'pressure pad detected'         randommp3=random.choice(os.listdir("/home/pi/audio/random/"))         os.system('omxplayer ' + randommp3)         print randommp3         sleep(5);


raspberrypi



Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support