I want to create a simple program that has two loops and I can change between them by pressing a physical button, like a mode selector.
MODE 1: Heating
MODE 2: Cooling
import RPi.GPIO as GPIO
import time
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
mode = 'heating'
def my_callback(channel):
if mode == 'heating'
mode = 'cooling'
print 'turned on cooling'
elif mode == 'cooling'
mode = 'heating'
print 'turned on heating'
GPIO.add_event_detect(21, GPIO.RISING, callback=my_callback)
while mode == 'heating'
print 'I am heating'
time.sleep(1.0)
while mode == 'cooling'
print 'I am cooling'
time.sleep(1.0)
When I run this code it starts with the heating mode, when I push the button the callback runs but the variable does not change and the heating loop is still running.