element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum How to break out of a while True: loop with a button
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 22 replies
  • Subscribers 675 subscribers
  • Views 10313 views
  • Users 0 members are here
  • raspberry_pi
Related

How to break out of a while True: loop with a button

flypadre
flypadre over 9 years ago

I've been working on some python scripts accessing the gpio pins on my rpi to light an led and I ran into a little problem I'm not sure how to solve.

My code looks something like this minus some setup statements

 

def ledblink():

     while True:

          GPIO.output(13, True)

          time.sleep(.5)

          GPIO.output(13, False)

          time.sleep(.5)

 

def ledoff():

     GPIO.output(13, False)

 

button = Button(root, text = 'LED ON', command = ledblink)

button.pack()

offbutton = Button(root, text = 'LED OFF', command = ledoff)

offbutton.pack()

 

As you can probably tell using tkinter for my gui. Also, as you can probably tell once I click the on button, that is all this program is going to let me do, the led blinks and continues because True is always True. How can I keep an indefinite loop running so that I can have a blinking led and still be able to break out of the loop when I want to turn things off? This was easy when I wanted to simply turn the led on and off, but not so easy with the loop I have to make it blink.

Just an FYI I was able to break out of the loop in my script version by using a try/except in which I used KeyboardInterrupt to call GPIO.cleanup() but I'm not sure how to do this in a gui.

 

 

 

  • Sign in to reply
  • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago

    You simply need to track the last button pressed in a variable and include this in your main while clause.

    as in

     

     

    While lastbuttonpressed == ledblink......

    {

    GPIO.output(13, True)

              time.sleep(.5)

              GPIO.output(13, False)

              time.sleep(.5)

     

    lastbuttonpressed == testbuttons()

    }

     

    if the button is detected as not ledblink button then it will exit

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago

    To expand on Robert Peter Oakes answer, provided the buttons in the tkinter library are running within seperate threads which I imagine they are, the following code should work. If they don't work in separate threads, the code will be too busy in the loop to break out of

     

    blinking = 1
    def ledblink():
         blinking = 1
         while (blinking == 1):
              GPIO.output(13, True)
              time.sleep(.5)
              GPIO.output(13, False)
              time.sleep(.5)
          GPIO.output(13,false)
    
    def ledoff():
         blinking = 0
    
    button = Button(root, text = 'LED ON', command = ledblink)
    button.pack()
    offbutton = Button(root, text = 'LED OFF', command = ledoff)
    offbutton.pack()

     

    Consider trying to use a thread to blink the led rather than a while loop, the Python interpreter will most likely be intelligent enough to yield system resources during the time.sleep calls but putting it into a thread and yielding the thread during the sleep period would ensure this and possibly use less processor time.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rew
    0 rew over 9 years ago in reply to Former Member

    lucie tozer wrote:

    Consider trying to use a thread to blink the led rather than a while loop, the Python interpreter will most likely be intelligent enough to yield system resources during the time.sleep calls but putting it into a thread and yielding the thread during the sleep period would ensure this and possibly use less processor time.

    This sort of performance advice is tricky. Sometimes the actual performance is quite different than you expect. As a matter of fact, we're blinking a led with a full-blown Linux system. A much simpler ARM CPU like an STM32F405 can blink a led. Wait-a-minute! A much simpler embedded CPU like an ATMEGA328 can also blink a led.... Wait-a-minute! A much simpler CPU like a pic10Fxxx can also blink a led.


    So in fact, we're using a several (*) orders of magnitude larger system to blink a led than is actually required.


    The reason we're using such an overpowered system is that we like the convenience of not having to worry about memory use or being wasteful of CPU cycles. So, first goal should be: Get things to work. Optimizing things is for later. Have fun seeing that led actually blink. Learn the basics. Later on, when things have become a big mess and even an overpowered Linux machine can't handle what you're throwing at it, does it make sense to start optimizing. But again: be warned that measurements as to what actually improves performance and what does not, can be surprising even to people who have been working in the field for decades.

     

    So with MY decades of experience, I would think that it would be difficult to measure the difference between the separate thread or doing it the other way. But if there is a difference, I'd say the advantage goes the other way.... But I'll acknowledge that if it would matter for me, I'd have to do the measurements and if my initial guess is wrong, I wouldn't be surprised.

     

    (*) After posting I decided to do a little math to verify my claim. If we look at "memory" as a measure for the "size" of a computing system, we have the pic at 100-200 bytes, the AVR one order of magnitude larger: 2k, the STM at two orders above that, 200k, and the raspberry pi with 1G over three orders of magnitude above that! A total of about 6 orders-of-manitude! A factor of a million! Wow!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to rew

    Im not entirely certain where you think I would be suggesting using a different processor or microcontroller or why providing some math on "memory" of these other components are relevant.

     

    I was helpful enough to provide a replacement code, what followed was merely a suggestion for a future improvement and something that I would probably do myself. tbh much of your reply is just blowing a simple suggestion out of proportion.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rew
    0 rew over 9 years ago in reply to Former Member

    Sorry.

    A famous saying goes "premature optimization is the root of all evil".

    We're using a processor that is (in some respects) 1 million times more powerful than what is required for the task. Optimizing this is not necessary. Moreover, my GUESS is that your optimization is counter productive (but only a little).

     

    So, IMHO, a more valid suggestion for "future improvement" would be: "Maybe putting the blink-a-led in a separate thread makes the code more readable. If you want to experiment with that you could try that".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • flypadre
    0 flypadre over 9 years ago

    OK This is what I thought I was being asked to do with regards to coding this without using threads, but this does not work either.

    import tkinter as tk
    import time
    
    root = tk.Tk()
    switch = False
    
    def blink(switch):
        while switch == True:
            print('BLINK...BLINK...')
            time.sleep(0.5)
        while switch == False:
            break
    
    def switchon():
        switch = True
        blink(switch)
    
    def switchoff():
        switch = False
        blink(switch)
    
    def kill():
        root.destroy()
    
    onbutton = tk.Button(root, text = "Blink ON", command = switchon)
    onbutton.pack()
    offbutton =  tk.Button(root, text = "Blink OFF", command = switchoff)
    offbutton.pack()
    killbutton = tk.Button(root, text = "EXIT", command = kill)
    killbutton.pack()
    
    root.mainloop()

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rew
    0 rew over 9 years ago in reply to flypadre

    Sean,

    The "use threads" suggestion was a "future improvement" because it "might improve the performance". I tried to say that "improving performance" at this stage is NOT a good idea for two reasons. 1) I don't think it actually improves performance and 2) is detracts from the issue of trying to get things to work.

     

    I am not very familiar with the language you're using. But the way you've written the blink function is never going to work.

     

    You are calling the blink function, with either a true or a false argument. In the first case (true) the while switch==true will ALLWAYS be true and remain true, so the loop can never end. In the second case (false), when you call the function the while loop will simply never execute and the second while loop will issue a useless "break" function.

     

    IF you get threads to work, and IF they work in python the way they work in C, then....

    [code]

    def blink:

        while True:

             if (switch) print ('blink')

             time.sleep (0.5)

    [/code]

    Now YOU have to figure out how to make this a thread. (I googled this for you and it is too complicated for me to understand in 5 minutes).  This function will continue to run, even when switch is false, but then it won't do the blink. 

     

    You may have to do some python magic to make the "switch" variable a global variable.

     

    Now when your program starts you do create_thread (blink) (however that is written in python). When the "start blinking" button is pressed you simply set switch=True, and when stop blinking button is pressed you simply do switch=False;

     

    When "quit" is pressed you'd better somehow "kill" the thread that does the blinking.

     

    Another way to implement this is to simply CREATE the thread doing the blinking when the start button is pressed and to stop the thread when either the stop or quit button is pressed.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to rew

    That code doesn't work very well for a responsive interface, you would need to be holding the button down throughout the sleep cycle in order for it to be tested when the if statement comes back around (for how long depends on when in the sleep cycle you started pressing the button. The worst case scenario would be pressing it for 0.5s which is a lot longer than anybody would like out of an on screen button. Also the light would only blink if the button was being held down). Not to mention that keeping the loop going when its not needed to blink the button is wholly inefficient and im sure you can agree that is just plain bad practice.

     

    The code I provided above is a direct drag and drop replacement for the original that doesnt use threading, im not going to say its the most efficient but it is working code that I tested before I posted and easy to read (for the sake of argument the performance increase of threading comes from not locking up a linux process and allowing it to be free'd up to do something else during the python threads sleep time, like draw the button to the screen efficiently, handle its file system, draw the desktop, send/recieve network data. All things that would benefit, when I talk about these things Im looking from an overall system advantage not just a narrow view of a single python script)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to flypadre

    There are a few redundant parts used here, try:

     

        import tkinter as tk 

        import time 

         

        root = tk.Tk() 

        switch = False 

         

        def blink(): 

            while switch == True: 

                print('BLINK...BLINK...') 

                time.sleep(0.5)   

         

        def switchon(): 

            switch = True 

            blink() 

         

        def switchoff(): 

            switch = False 

            blink() 

         

        def kill(): 

            root.destroy() 

         

        onbutton = tk.Button(root, text = "Blink ON", command = switchon) 

        onbutton.pack() 

        offbutton =  tk.Button(root, text = "Blink OFF", command = switchoff) 

        offbutton.pack() 

        killbutton = tk.Button(root, text = "EXIT", command = kill) 

        killbutton.pack() 

         

        root.mainloop()

     

    ------------------------------------------------------------------------------------------------------

    Theres no need to add the switch into the function definition arguments, because its already being declared outside the functions its already globally used in the script and is causing confusion when declaring blink() function.

     

    When I get chance later this afternoon I'll recreate a working example again and post the entire script rather than just a snippet so you can see it working. Where about are you looking for the "blink.....blink" text to be printed? It should come out in the console you used to launch your script (if you launch it without using a console/terminal you probably wont ever see the text)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 9 years ago in reply to flypadre

    My original test program also had the tkinter buttons in a thread so they were working independently. There is a very good chance that without using a thread at least once you will never be able to break out of the while loop in a responsive manner. Would you mind if I provided an example using threads? its really just going to be about 4 extra lines, with one of them being the import threads statement and is easy to understand. It would make your task so much simpler.

     

    For a responsive buttons without any threading you are going to have to add a for loop which contains a sleep function for a miniscule amount of time and a call to yield within the loop, then another master loop to control it all. Without using threads the code is going to need about 20 more lines to achieve this task

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube