Darth Tader

Darth Nihilus voice program.

27 posts in this topic

Found a some sounds files, and I set mine up as follows.

Assumptions based on how I set it up this weekend:

  • One push button configuration, used with two different setups (one-click and in development, multi-click) on glove. I put buttons between my left-hand thumb and index finger to minimize accidental pressing.
  • GPIO Pin 23 will be used for button
  • Files are MP3 (number is only limited by memory, I had 39):
    • Format: #X.mp3
      • Files were numbered 1-39, then length added (S/M/L) then .mp3. For example: '1L.mp3' for the first file and is long. This will allow filtering 
  • MP3 files are saved in '/home/pi/nihilus'
  • Script can be saved anywhere but I had it in '/home/pi/playback.py'
  • crontab is set up as per my last post to run on reboot (every boot)

This is the code I used for a single push button; with a push triggering one of the random 39 files:

#!/usr/bin/env python
#One push button setup:

import os, glob, random, RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

os.chdir("/home/pi/nihilus")
files = glob.glob("*.mp3")

while True:
    if (GPIO.input(23) == False):
        os.system("mpg123 -q " + random.choice(files))

    sleep(0.1)

Prototype: (not confirmed working yet): From here, I'm working on using the one button to do 3 actions: 1 press for short files, 2 presses for medium files, and 3 presses for long files. I am waiting to get my new push button before fine tuning this:

import os, glob, random, time, RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

# Read in files
os.chdir("/home/pi/nihilus")
shortfiles = glob.glob("*S.mp3")
mediumfiles = glob.glob("*M.mp3")
longfiles = glob.glob("*L.mp3")

while True:
	if GPIO.event_detected(23):
		GPIO.remove_event_detect(23)
		now = time.time()
		count = 1
		GPIO.add_event_detect(23,GPIO.RISING)
		while time.time() < now + 1: # 1 second period
			if GPIO.event_detected(23):
				count +=1
				time.sleep(.25) # debounce time
		if count == 2:
			os.system("mpg123 -q " + random.choice(shortfiles))
			GPIO.remove_event_detect(23)
			GPIO.add_event_detect(23,GPIO.FALLING)
		elif count == 3:
			os.system("mpg123 -q " + random.choice(mediumfiles))
			GPIO.remove_event_detect(23)
			GPIO.add_event_detect(23,GPIO.FALLING)
		elif count == 4:
			os.system("mpg123 -q " + random.choice(longfiles))
			GPIO.remove_event_detect(23)
			GPIO.add_event_detect(23,GPIO.FALLING)

 

Edited by cooldevo
updated clarity in a couple of spots

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now