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:
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 :
Thank you a lot!
Moderator edit: added code tags
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
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