• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 104
  • Last Modified:

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:

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


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'):
    print ("was turned on")
elif var1 == ('off'):
    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 Rich
Zeke Rich
2 Solutions
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
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.
Zeke RichOwnerAuthor Commented:
Thank you very much! The problem/issue was correct! Key Bounce. After adding

Open in new window

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

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now