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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.