Hello, I am pretty new to python and coding, so this may be a basic question, but how would I go about exiting a loop with a button press via gpio input?
What I want is once a condition is triggered (will eventually be triggered by sensors, but currently for testing and learning is triggered by a button press via gpio input) a led will flash on and off until a button via gpio input is pressed.
The snippet of code I am tinkering with:
alarmtrigger() is a thing I was testing to get the LED flashing, but kinda already knew it probably wouldn't work... but it is as follows:
I also tried a while true loop, but since I am already using a while true loop to detect multiple buttons constantly, a second while true loop isnt possible (or I think it isnt from what I have googled least ways)
So I'm a little stumped on what I need to do to get the results I want.
Here is the code in full for those who want it:
Thanks to all that help me figure this out!!
What I want is once a condition is triggered (will eventually be triggered by sensors, but currently for testing and learning is triggered by a button press via gpio input) a led will flash on and off until a button via gpio input is pressed.
The snippet of code I am tinkering with:
Code:
#RED BUTTONif GPIO.input(17) == GPIO.HIGH: print("Red button was pressed") display.lcd_display_string("Red button pressed", 1) alarmtrigger()elif GPIO.input(23) == GPIO.HIGH: break
Code:
def alarmtrigger(): GPIO.output(14, 1) sleep(.5) GPIO.output(14, 0) sleep(.5) alarmtrigger()
So I'm a little stumped on what I need to do to get the results I want.
Here is the code in full for those who want it:
Code:
# Import necessary libraries for communication and display useimport osimport driversfrom time import sleepimport RPi.GPIO as GPIO#VARIABLESdisplay = drivers.Lcd()#GPIO SET UPGPIO.setmode(GPIO.BCM)GPIO.setup(14, GPIO.OUT)GPIO.output(14, 0) #ALARM LEDGPIO.setup(15, GPIO.OUT) #ALARM POSGPIO.output(15, 1)#GPIO.setup(4, GPIO.OUT) #NULL#GPIO.output(4, 0) #NULL#GPIO.setup(4, GPIO.IN) #NULLGPIO.setup(17, GPIO.OUT)GPIO.output(17, 0) # BUTTON REDGPIO.setup(17, , pull_up_down=GPIO.PUD_DOWN)GPIO.setup(18, GPIO.OUT)GPIO.output(18, 0) #BUTTON BLUEGPIO.setup(18, , pull_up_down=GPIO.PUD_DOWN)GPIO.setup(27, GPIO.OUT)GPIO.output(27, 0) #BUTTON GREENGPIO.setup(27, , pull_up_down=GPIO.PUD_DOWN)GPIO.setup(22, GPIO.OUT)GPIO.output(22, 0) #BUTTON YELLOWGPIO.setup(22, , pull_up_down=GPIO.PUD_DOWN)GPIO.setup(23, GPIO.OUT) #ALARM NEGGPIO.output(23, 0)GPIO.setup(23, , pull_up_down=GPIO.PUD_DOWN)GPIO.setup(24, GPIO.OUT) #BUTTON POSGPIO.output(24, 1)GPIO.setup(25, GPIO.OUT)GPIO.output(25, 0)GPIO.setup(10, GPIO.OUT)GPIO.output(10, 0)GPIO.setup(9, GPIO.OUT)GPIO.output(9, 0)GPIO.setup(6, GPIO.OUT)GPIO.output(6, 0) GPIO.setup(12, GPIO.OUT)GPIO.output(12, 0) #LED BLUEGPIO.setup(13, GPIO.OUT)GPIO.output(13, 0) #LED YELLOWGPIO.setup(16, GPIO.OUT)GPIO.output(16, 0) #LED GREENGPIO.setup(19, GPIO.OUT)GPIO.output(19, 0) #LED REDGPIO.setup(20, GPIO.OUT)GPIO.output(20, 0) #LED WHITEGPIO.setup(21, GPIO.OUT)GPIO.output(21, 0) #LED PURPLEdef clear(): os.system('clear')def alarmtrigger(): GPIO.output(14, 1) sleep(.5) GPIO.output(14, 0) sleep(.5) alarmtrigger()################################# Main body of codeclear()try: while True: display.lcd_clear() #print("LCD Cleared") GPIO.setmode(GPIO.BCM) #print("GPIO Prepped")#BUTTON PRESS #RED BUTTON if GPIO.input(17) == GPIO.HIGH: print("Red button was pressed") display.lcd_display_string("Red button pressed", 1) alarmtrigger() elif GPIO.input(23) == GPIO.HIGH: break #BLUE BUTTON if GPIO.input(18) == GPIO.HIGH: print("Blue button was pressed") display.lcd_display_string("Blue button pressed", 1) GPIO.output(14, 1) sleep(.5) GPIO.output(14, 0) #GREEN BUTTON if GPIO.input(27) == GPIO.HIGH: print("Green button was pressed") display.lcd_display_string("Green button pressed", 1) GPIO.output(14, 1) sleep(.5) GPIO.output(14, 0) #YELLOW BUTTON if GPIO.input(22) == GPIO.HIGH: print("Yellow button was pressed") display.lcd_display_string("Yellow button pressed", 1) GPIO.output(14, 1) sleep(.5) GPIO.output(14, 0) #ALARM BUTTON if GPIO.input(23) == GPIO.HIGH: print("Alarm button was pressed") display.lcd_display_string("Alarm button pressed", 1) GPIO.output(14, 1) #sleep(5) pygame.mixer.init() pygame.mixer.music.load("AlarmAcknowledged.mp3") pygame.mixer.music.set_volume(1.0) pygame.mixer.music.play() GPIO.output(14, 0) sleep(.5)except KeyboardInterrupt: clear() print("Cleaning up!") print("") display.lcd_clear() print("Clearned LCD Screen") sleep(.1) GPIO.cleanup() print("Cleaned GPIO Pins") sleep(.1) print("") print("Exiting Script") print("") sleep(.5) print("Thank you, come again!") sleep(.5) clear()
Statistics: Posted by jfisher1991 — Thu Nov 14, 2024 12:49 am