Add variable to function in Python

I need to add the variable var2 to a function:

This does not work:
var2 = pin25
var2+.start(100)

Open in new window


This does work:
pin25.start(100)

Open in new window

Zeke RichOwnerAsked:
Who is Participating?
 
Flabio GatesCommented:
ok, I think I understand what you are trying to do so try something like:
#!/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(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 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
 
LOOKUP = {
    "pin23": pin23,
    "pin24": pin24,
    "pin25": pin25
    } 
 
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
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)
 
if var1 == 'on':
    LOOKUP[var2].start(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 var1 == 'off':
    LOOKUP[var2].stop()
    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

where the important parts are
LOOKUP = {
    "pin23": pin23,
    "pin24": pin24,
    "pin25": pin25
    } 

Open in new window

and
    LOOKUP[var2].start(100)

Open in new window

and
    LOOKUP[var2].start(100)

Open in new window


Basically what I did was create a dictionary mapping the string values in var2 to the actual pin objects.
0
 
Flabio GatesCommented:
so why not just use
var1.start(100)

Open in new window

what did you expect to accomplish with the plus (+) sign?
0
 
Zeke RichOwnerAuthor Commented:
Flabio I have only been programming in Python for 1 week so im not really sure what i am doing. When i try var2 it does not do anything and does not print also.

This is my working code. it works perfect with pin25.start(100) but when i try to change that to var2 it no longer works:

#!/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(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 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
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)

 
if var1 == 'on':
    pin25.start(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 var1 == 'off':
    pin25.stop()
    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

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Zeke RichOwnerAuthor Commented:
I am now getting this error:
 sudo python /home/pi/Desktop/light.py on 25
Received from variable 1: "on"
Received from variable 2: "25"
Traceback (most recent call last):
  File "/home/pi/Desktop/light.py", line 44, in <module>
    var2.start(100)
AttributeError: 'str' object has no attribute 'start'

Open in new window


From This script using var2:
#!/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(25, 100)        #set the PWM on pin 25 to 100 hrz / full power
pin24 = GPIO.PWM(25, 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
 
var1 = sys.argv[1].strip() #get the on/off variable sent from php
var2 = sys.argv[2].strip() #get the GPIO Pin variable sent from php on/off
 
print('Received from variable 1: "%s"' % var1)
print('Received from variable 2: "%s"' % var2)

 
if var1 == 'on':
    var2.start(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 var1 == 'off':
    var2.stop()
    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

0
 
Flabio GatesCommented:
oh and in your original post, the following
var2 = pin25
was misleading because var2 is not equal to pin25 but is instead equal to the string "pin25"
the difference is crucial
0
 
Zeke RichOwnerAuthor Commented:
Exactly the solution i needed!!! Thank you, you saved the day!
0
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.

All Courses

From novice to tech pro — start learning today.