Script terminates when new call comes in.

Hi , I am a python beginner,

When i call this script it turns on a led light, but when i try to turn on a second or third light, the first light turns off. It will only turn on one light at a time and turns off the prior light. How can i turn on all 3 lights at the same time? Thank you!

The Call from command line
sudo python /home/pi/Desktop/light_0.py pin23 on

Open in new window


The script
#!/usr/bin/python
 
import RPi.GPIO as GPIO        #This line alone caused 90 minutes of frustration
import sys
import time
import fcntl  #To lock this script

#Begin to lock the file
pid_file = 'LOCKFILE-FOR-THIS-SCRIPT.pid'
fh = open(pid_file, 'w')

try:
    fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    print ('Error: Another instance is running...')
    sys.exit(0)

# FOLLOWING IS THE NORMAL CODE 
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
GPIO.setup(23, GPIO.OUT) #set pin 21 to output
GPIO.setup(24, GPIO.OUT) #set pin 21 to output
GPIO.setup(25, GPIO.OUT) #set pin 21 to output
 
pin23 = GPIO.PWM(23, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(24, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin25 = GPIO.PWM(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
 
pin23.start(0) #start the pwm at 0 percent power
pin24.start(0) #start the pwm at 0 percent power
pin25.start(0) #start the pwm at 0 percent power

LOOKUP = {
    "pin23": pin23,
    "pin24": pin24,
    "pin25": pin25
    }
 
pin_number = sys.argv[1].strip() #pin number
action = sys.argv[2].strip() #action on/off
 
print('Received pin_number: "%s"' % pin_number)
print('Received action: "%s"' % action)
 
if action == 'on':
    LOOKUP[pin_number].ChangeDutyCycle(100)
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
	
    print("was turned on")
	
elif action == 'off':
    LOOKUP[pin_number].ChangeDutyCycle(0)
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic
	
    print("was turned off")
	
else:
    print("unknown command")

Open in new window

Zeke RichOwnerAsked:
Who is Participating?
 
gelonidaCommented:
can't test as I don't have the hardware, but try this


#!/usr/bin/python

import RPi.GPIO as GPIO

import sys
import time
import fcntl  #To lock this script

#Begin to lock the file
pid_file = 'LOCKFILE-FOR-THpythonIS-SCRIPT.pid'
fh = open(pid_file, 'w')

try:
    fcntl.lockf(fh, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
    # another instance is running
    print ('Error: Another instance is running...')
    sys.exit(0)

# FOLLOWING IS THE NORMAL CODE 

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

def mk_pin(pinnum):
    GPIO.setup(pinnum, GPIO.OUT) #set pin pinnum to output
    pin = GPIO.PWM(pinnum, 100)        #set the PWM on pin 25 to 100 hrz / full power
    pin.start(0) #start the pwm at 0 percent power
    return pin

LOOKUP = {
    "pin23": 23,
    "pin24": 24,
    "pin25": 25
    }

pin_name = sys.argv[1].strip() #pin number
action = sys.argv[2].strip() #action on/off

 
print('Received pin_name: "%s"' % pin_name)
print('Received action: "%s"' % action)

pin_number = LOOKUP[pin_name]

pin = mk_pin(pin_number)
if action == 'on':
    pin.ChangeDutyCycle(100)
    print("was turned on")
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic

elif action == 'off':
    pin.ChangeDutyCycle(0)
    print("was turned off")
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic

elif action == 'init':
    print("was initialized (and probably turned off")
    # not sure if you need the sleep here. I can't try it out
    time.sleep(0.5) #if you dont give it enough time to recover it will keyskip and act erattic

else:
    print("unknown command", action)

Open in new window

1
 
gelonidaCommented:
The problem is that this python script configures and initializes always all of the three pins.


for example for pin 23 you have lines
24, 28, 32 which are called even if you pass another pin number.

you could rewrite your code such, that it only initializes the pin that you actively want to change or you could add a special 'command', that initializes all pins, which you have to call once whenever you switch on the raspberry pi/


example:
 sudo python /home/pi/Desktop/light_0.py pin23 init
 sudo python /home/pi/Desktop/light_0.py pin24 init
 sudo python /home/pi/Desktop/light_0.py pin25 init
 sudo python /home/pi/Desktop/light_0.py pin23 on
 sudo python /home/pi/Desktop/light_0.py pin24 on
 sudo python /home/pi/Desktop/light_0.py pin23 off

Open in new window

0
 
Zeke RichOwnerAuthor Commented:
WOWOWOW =0 Its a miracle! Its working! Thank you so much!!!!! Happy New year! You save my life! Incredible solution! Amazing Python Expert!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.