Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Instruction Guide Use hardware momentary switches to control moOde
#9
I have changed your code to interrupt and debouncing, as it sometimes did not react fast enough. And added time to track bouncing in the serial monitor. No hardware debouncing needed (no pull resistors, capacitors). Works more smoothly now:

Code:
#!/usr/bin/python
#coding: utf8
#script for moode audio GPIO buttons
#on playlist names no spaces

#import
import RPi.GPIO as GPIO
import sys
import time
import datetime
import os
import subprocess

#set GPIO mode instead of GPIO.setmode(GPIO.BOARD) - Zählweise der Pins festlegen
GPIO.setmode(GPIO.BCM)

#Define your GPIO pins
SW_PREV = 10
SW_NEXT = 11
SW_POWEROFF = 9
SW_PLAY = 12
#SW_VOLUP = 20
#SW_VOLDOWN = 26
#SW_PL1 = 16
#SW_PL2 = 13
PRELL = 1000          # Adjust to the button characteristics if necessary

#code to manage BUTTONS
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_POWEROFF,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(SW_VOLUP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(SW_VOLDOWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(SW_PL1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#GPIO.setup(SW_PL2,GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Ereignis-Prozedur für SW_PREV
def eSW_PREV(channel):
       subprocess.call(['mpc', 'prev' ])
       print str(datetime.datetime.now())[:19] + " previous"

def eSW_NEXT(channel):
       subprocess.call(['mpc', 'next' ])
       print str(datetime.datetime.now())[:19] + " next"

def eSW_POWEROFF(channel):
       subprocess.call(['mpc', 'stop' ])
       subprocess.call(['sudo', 'poweroff' ])
       print str(datetime.datetime.now())[:19] + " shutting down"

def eSW_PLAY(channel):
       subprocess.call(['mpc', 'toggle' ])
       print str(datetime.datetime.now())[:19] + " play"        
       
# Ereignis deklarieren
GPIO.add_event_detect(SW_PREV, GPIO.RISING, callback = eSW_PREV, bouncetime = PRELL)
GPIO.add_event_detect(SW_NEXT, GPIO.RISING, callback = eSW_NEXT, bouncetime = PRELL)
GPIO.add_event_detect(SW_POWEROFF, GPIO.RISING, callback = eSW_POWEROFF, bouncetime = PRELL)
GPIO.add_event_detect(SW_PLAY, GPIO.RISING, callback = eSW_PLAY, bouncetime = PRELL)

# Eigentlicher Programmablauf
while True:
   time.sleep(1)
Reply


Messages In This Thread
RE: Use hardware momentary switches to control moOde - by Cyanoazimin - 03-03-2019, 06:29 PM

Forum Jump: