Python break out of while loop problem

Thread Starter

Leo17

Joined Nov 18, 2020
16
Hello! I am quite new with Python and i m trying to program my Raspberry pi to display temp and also open and close the lcd using the buttons. Every code works in particular but when i try to get them all together i cannot get out of an while loop so the program remains on displaying the temperature and cannot open/close the display
the code looks like this:
Code:
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import time
import lcddriver
import Adafruit_DHT as dht
display = lcddriver.lcd()

try:
    while True:
        humi, temp = dht.read_retry(dht.DHT22, 4)
        print("Writing to display")
        display.lcd_display_string('Temp: %d C' % temp, 1) # Write line of text to first line of display
        display.lcd_display_string('Humi: %d %%' % humi, 2) # Write line of text to second line of display
        time.sleep(3)                                     # Give time for the message to be read

except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
    print("Cleaning up!")
    display.lcd_clear()

def button_callback_one(channel):
    print("S-a pornit ecranul")
    time.sleep(1.5)
    execfile("ledv.py")
    execfile("pornit.py")
    time.sleep(1.5)
    #execfile("temp_display.py")
def button_callback_two(channel):
    print("S-a oprit ecranul")
    execfile("ledr.py")
    execfile("oprit.py")
    time.sleep(1.5)
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback_one) # Setup event on pin 10 rising edge
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(40,GPIO.RISING,callback=button_callback_two) # Setup event on pin 10 rising edge

message = input("Press enter to quit\n\n") # Run until someone presses enter

GPIO.cleanup() # Clean up
any idea on how to break out of the while loop while pressing "button_callback_one" and also close the program while i press button_callback_two?
the temp display program is the first part of the program which is :

Code:
try:
    while True:
        humi, temp = dht.read_retry(dht.DHT22, 4)
        print("Writing to display")
        display.lcd_display_string('Temp: %d C' % temp, 1) # Write line of text to first line of display
        display.lcd_display_string('Humi: %d %%' % humi, 2) # Write line of text to second line of display
        time.sleep(3)                                     # Give time for the message to be read

except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
    print("Cleaning up!")
    display.lcd_clear()

Thank you a lot!

Moderator edit: added code tags
 

MrChips

Joined Oct 2, 2009
30,707
Instead of while True
replace True with some condition, for example

while running

modify running with some logic in your code when you want to remain in the loop.
 

vanderghast

Joined Jun 14, 2018
67
I am not too familiar with Python, but if there is a possibility to handle interruptions, remember to share a VOLATILE variable between the main process and the interrupt-handler (I don't know which keyword matches the volatile concept in Python). And by volatile, I mean that your code should be aware that the value can be modified by another process and so, that it should not be "buffered locally" by the compiler (if Python does that) ; the "compiler" may optimize the code by pushing a value into a register, pushed on the stack, hidden by some declaration, etc., rather than reading it from the standard memory each time it needs it, which is slower but here, that is required since it may have been changed (by another process). And so, in your code, your while loop may explicitly checks if an interesting volatile flag has been raised. If so, the while loop will end and your code can check, exiting the while loop, which interesting flag was raised and requires to be handled. Note that while it is possible to exit (the program) from the code handling the interrupt, that is unlikely universally applicable since doing so, you may miss an important continuation (such as servicing a pending interrupt of a lower priority).
 
Top