danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

functions in Python

Fri Mar 15, 2013 9:10 am

I am obviously pretty new to Python and I cannot for the life of me access my function within my code.

to note:button7 is a GPIO pin that is properly declared/tested with exact same setup outside of a function.


I am just not sure why my function never gets called. Have read Python documentation but its a no go for me.

Code: Select all

#below is my function only
def buttons(button7):
    
    #while True:
        
        if (button7==1):
        #if(a >70 and a < 240): widths=400 heights=400 #del(capture) stop
            print("HIGH MODE")
            #cv.Resize(face_region,640,480)        
            cv.SaveImage('/home/pi/tester.png', face_region )
            #capture = None
            
            #time.sleep(5) #dont overbuffer same image
            imin = Image.open('/home/pi/tester.png') #was im.open
            imout=imin.resize((widths,heights),Image.BICUBIC)
            imout.save('/home/pi/test_resized.png')
            #twitter.updateStatusWithMedia('/home/pi/test_resized.png', status='#chicagofaces')
            print('IMAGE SENT TO TWITTER');
            GPIO.output(25, False)
            GPIO.output(24, True)
            #time.sleep(60)#calculated how many tweets allowed. 1000 limit per day. 1 every 1.5min
            os.system("sudo rm /home/pi/test_resized.png && sudo rm /home/pi/tester.png");
            #del(capture)
            #GPIO.output(25, True)
            #GPIO.output(24, False)
            free_loop=True
        #return

        if (button7==0):
    #if (free_loop==True):
            GPIO.output(25, True)
            GPIO.output(24, False)
            free=True
        return;
buttons(button7);

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: functions in Python

Fri Mar 15, 2013 9:17 am

If your function is never called, then the function is not the the problem, the lines where the function is called are the problem areas, no doubt an if condition or other logic error. Even possibly not being called because you forgot to put the call in.

How have you determined the function is not being called

The code for the function you have put up appears to have tab indent issues of unexpected extra or missing tabs so when called will give errors.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: functions in Python

Fri Mar 15, 2013 9:22 am

I'd suggest to first start with a Python tutorial so that you can get a good understanding of the basics (functions, etc), and then move on to the real project. "Learn Python the Hard Way" is a free book for beginners -- this is the section about functions: http://learnpythonthehardway.org/book/ex19.html
pythonhosted.org/RPIO

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: functions in Python

Fri Mar 15, 2013 3:29 pm

Firstly, commented-out lines of code make it hard for us to read, and make it easy to miss bugs. If you need to post any more code, remove the commented-out lines first.

It looks like your code will do something if 'button7' is the integer value 1, and do something else if it's the integer value 0. It could be many other things, in which case the function would return silently.

To find out what 'button7' is, try:

Code: Select all

print repr(button7)
repr() is a handy built-in function which gives you the Python representation of a particular value - it lets you distinguish numbers, strings, lists, and so on.
-----
https://github.com/IanHarvey

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: functions in Python

Fri Mar 15, 2013 3:59 pm

It's difficult to work out but it looks like the line

#twitter.updateStatusWithMedia('/home/pi/test_resized.png',status='#chicagofaces')

being commented out is the problem. Because of Python's indentation all the code below the above line will be included in your function definition and button7 will never equal 1.
I'd take metachris' advice and read up on functions. I've only been coding in Python for a shortish while, BASIC and C before that and must say the indentation thing can be a pain until you get used to it.

Hope this sorts something out for you.

Regards.
Dave.
Apple say... Monkey do !!

danieljay
Posts: 49
Joined: Sun Sep 16, 2012 5:01 pm
Contact: Website

Re: functions in Python

Fri Mar 15, 2013 6:56 pm

thanks for all the advice. I know (well....ASSUME) since the code compiles fine, when I press the button, nothing is happening. I am not recieving indention errors or and others in the IDE or terminal

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: functions in Python

Fri Mar 15, 2013 10:27 pm

danieljay wrote:thanks for all the advice. I know (well....ASSUME) since the code compiles fine, when I press the button, nothing is happening. I am not recieving indention errors or and others in the IDE or terminal
So if the function is not being called maybe we need to see HIGHER up as to what is happening and how you have wired up the switches.

If the function is NOT being called then the contents of the function are probably NOT the problem.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

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

Re: functions in Python

Sat Mar 16, 2013 10:45 pm

danieljay
I copied your code verbatim to geany on this ubuntu laptop and just changed the last line to 'buttons(0)' or 'buttons(1)' and it seemed to run ok. (apart from the fact that obviously none of the GPIO stuff made any sense)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: functions in Python

Sun Mar 17, 2013 12:05 am

I would start debugging the code by putting a print statement in the beginning of the function. If it gets executed, you will know that the problem is inside the function and not in the code that is calling the function.

Return to “Python”