Link to home
Start Free TrialLog in
Avatar of Zeke Rich
Zeke RichFlag for United States of America

asked on

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

Avatar of gelonida
gelonida
Flag of France image

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

ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zeke Rich

ASKER

WOWOWOW =0 Its a miracle! Its working! Thank you so much!!!!! Happy New year! You save my life! Incredible solution! Amazing Python Expert!