Davros-
Posts: 48
Joined: Sun Aug 30, 2015 6:33 pm

Passing Variables as Arguments to Timer Objects and other Functions

Tue May 15, 2018 10:09 pm

Hello, all.

I need some quick help with the syntax for using variables as arguements to be passed around. This seemed like something I could look up but the info I read just does not work and usually it is not specific enough. As I am sure it will not take long for experienced Python coders to educate me I thought I would ask here.

My script controls the colours on an LED device in reaction to GPS cooridinates. It has colour variables so when I want to change the colours I just change the deifnition of the variable instead of retyping the RGB colour a bunch of times.

Code: Select all

colour1 = [254, 0, 0]
colour2 = [0, 0, 254]

# colour1 = [254, 0, 0]
# colour2 = [0, 0, 254]
If I want to switch red to blue and vice versa I comment out the first two lines and uncomment the last two lines.

I am using the Mote LED device from Pimoroni. Imported their library and am using the following function they provide.

Code: Select all

def changeColour(r, g, b):
    mote.clear()
    for channel in range(1, 5):
        for pixel in range(16):
            mote.set_pixel(channel, pixel, r, g, b)
    mote.show()

Tried using the variable in my main loop and had problems then it was explained to me how it should work and using changeColour(*colour1) did the tirck.

As my script runs way longer than it should I researched how to do things like take a pause without blocking everything hoping it would help with the timing. So I am experimenting with using timer objects. I do not know what, if any, is the correct way to put the arguments by means of my variable inside the timer object function. (Admitedly I really do not know if using timer objects will help at all.)

For example,

Code: Select all

t = Timer(3, changeColour, *colour1)
t.start()
does not work. Many other variations do not either. So I would like to know the correct syntax so I can pass my RGB values on as an arguement to the timer. I read one example and thought maybe I should be taking it literaly so I tried the following.

Code: Select all

t = Timer(3, changeColour, args=[colour1])
t.start()
No error messages but that just seems like it would not be correct.

Now though even though I made no changes to sections such as:

Code: Select all

if latitude > NorthLine:
    changeColour(*colour1)
I am suddenly getting error messages for any changeColour(*colour1) lines when I did not before.


Would appreciate knowing how to be able to use a variable to pass arguements to the changeColour() function from both normal lines inside the main loop and from any references to timer objects.

The whole script is at https://hastebin.com/opuvayacuz.py at least for a while.


I realize that Python just does not do accurate timing but I would like to improve it if possible. This Python beginner thanks you very much for any help provided.

Writting this in a big hurry rushing out to work. Apologies for poor typing/thought organization.

User avatar
paddyg
Posts: 2148
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Passing Variables as Arguments to Timer Objects and other Functions

Tue May 15, 2018 11:32 pm

I wouldn't use the * expansion - it can be rather confusing. It basically expands a list or tuple into separate arguments for passing to a function, which is why python complains when you use it as you call the function. I would pass the list like this, which is easier to understand until you become more expert.

Code: Select all

def changeColour(rgb):
    mote.clear()
    for channel in range(1, 5):
        for pixel in range(16):
            mote.set_pixel(channel, pixel, rgb[0], rgb[1], rgb[2])
    mote.show()

colour1 = [254, 0, 0]
colour2 = [0, 0, 254]
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Davros-
Posts: 48
Joined: Sun Aug 30, 2015 6:33 pm

Re: Passing Variables as Arguments to Timer Objects and other Functions

Thu May 17, 2018 9:01 pm

paddyg: Thank you for your reply. While I have other, bigger picture issues going on with my script this was a problem and you solved it. Worked out just fine. It also made me think more about lists, indexes, etc which I think gave me a better understanding of those.

Appreciate your help.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 10 guests