Zeke Rich
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
The script
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
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")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
WOWOWOW =0 Its a miracle! Its working! Thank you so much!!!!! Happy New year! You save my life! Incredible solution! Amazing Python Expert!
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:
Open in new window