Hello,
think there are two areas where your script needs to be improved.
One point is the arrangement of code
Code: Select all
if run == False:
os.system("nohup /home/pi/radio.sh &>/dev/null &")
time.sleep(4)
print()
run = True
time.sleep(0.2)
if run == True:
os.system("sudo pkill mplayer")
run = False
if the first "if run == False"- block, the run-variable is set to True. Which immediately results in the execution of the " if run == True:"-Block. So your mplayer gets stopped immediately after a start.
Here an 'else if '-condition for the second block would help.
The other point is the handling of button pressed-event. Think you want to achieve something like 'when button pressed first time, then switch on, when button pressed second, switch off' The unspoken event in between is to check for a button released condition.
Here a proposal for a state based approach. I used GPIO 3, change this back to your GPIO used. The quite long delay(0.1) is to debounce the button and avoids excessive cpu usage.
Code: Select all
import RPi.GPIO as GPIO
import time
debug = True
BUTTON_GPIO = 3
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO, GPIO.IN)
# the signals used to detect button pressed/released signals.
# here: button is connected to gnd (active is a low signal)
PRESSED = 0
RELEASED = 1
# the state enumerations are defined here as 'constants'. The development environment I use
# allows for variable lookup and thus avoids problem by misspelling
#
START = 'start'
B0_RELEASED = 'B0_RELEASED'
B0_PRESSED= 'B0_PRESSED'
B1_RELEASED= 'B1_RELEASED'
B1_PRESSED= 'B1_PRESSED'
state = START
nextState = None
while True:
time.sleep(0.1)
nextState = None
c = GPIO.input(BUTTON_GPIO)
if state == START:
if c == RELEASED:
nextState = B0_RELEASED
elif state == B0_RELEASED:
if c == PRESSED:
nextState = B0_PRESSED
print("do your switch ON radio action here")
elif state == B0_PRESSED:
if c == RELEASED:
nextState = B1_RELEASED
elif state == B1_RELEASED:
if c == PRESSED:
nextState = B1_PRESSED
print("do your switch OFF radio action here")
elif state == B1_PRESSED:
if c == RELEASED:
nextState = B0_RELEASED
if debug == True:
if nextState != None:
print("{state:s} --[ {c:d} ]--> {nextstate:s}".format(state=state, c=c, nextstate=nextState))
if nextState != None:
state = nextState
Regards,
Gerhard