Calling script more than once, works intermittently

I made a Python script to turn on and off a light. I call it from another script and pass a variable of 'on' or 'off' from a PHP script.  Its sometimes blinks the light on for a second, sometimes it turns it on. But its not working right. Is the script conflicting itself?

Im new to Python, do i need to quit or exit the script after im done running it?

My python script:
#!/usr/bin/python

import RPi.GPIO as GPIO        #This line alone caused 90 minutes of frustration
import sys
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(25, GPIO.OUT) #set pin 21 to output

p = GPIO.PWM(25,100)        #set the PWM on pin 25 to 100 hrz / full power

p.start(0) #I dont remember what this does but trust me you need it.

var1 = sys.argv[1] #get the variable sent from php on/off

print (var1)

if var1 == ('on'):
    p.ChangeDutyCycle(100)	
    print ("was turned on")
elif var1 == ('off'):
    p.ChangeDutyCycle(0)
    print ("was turned off")

Open in new window


These are attached to a button in PHP, i press it on and off fast and it works 5% of the time:
shell_exec("sudo python /home/pi/Desktop/light.py 'on' ")
shell_exec("sudo python /home/pi/Desktop/light.py 'off' ")

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.

David Johnson, CD, MVPOwnerCommented:
what you are seeing appears to be key-bounce behaviour. so you need to not accept input for a period of time i.e. pause 100ms
4

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
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
David's likely correct.

Hardware takes some time to settle, so if you try toggling state of your light before your hardware has settled, results become indeterminate. Try 100ms or refer to the specs on the specific gate chip or diode or however you're effecting your light to specific characteristics of your circuit, to allow long enough settling time to ensure correct operation 100% of the time.
0
Zeke RichOwnerAuthor Commented:
Thank you very much! The problem/issue was correct! Key Bounce. After adding
time.sleep(0.5)

Open in new window

The erratic behavior was corrected!
Great assistance, many thanks!
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.