I'm making a little automated process that activates a processor, lowers parts into it, waits for a period, deactivates the processor, and raises the parts. It is activated by pressing a switch (in real life it's actually 2 buttons and a safety switch), which is GPIO 2.
I'd like to add a physical stop button local to the process in case I want to interrupt it and change some parameters. I have the stop button wired to ground and GPIO 17 right now. I've added code at line 31 for the stop button, and I've tweaked it a few times, but it doesn't seem to want to work for me. The button I purchased I'm using is an e-stop button I had laying around, so it is normally open, and when pressed is closed. The button stays closed until you twist it and it resets.
The problem I'm having is it doesn't seem to work correctly, and triggers the process to stop when it shouldn't. Is it possible to get some guidance on how to properly implement this? The code is below. Thank you!
Code: Select all
from gpiozero import LED, Button import RPi.GPIO as GPIO from time import sleep import sys signal = LED(26) ##This is signal to turn on processor. !!THIS WORKS BACKWARDS!! down = LED(20) ##This is signal to move parts into bath. switch = Button(2) ##This is safety switch & 2 buttons in series. GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) while True: signal.on() ##Makes sure processor is off. stop_state = GPIO.input(17) ##State of the stop button, true or false. process = 480 ##This is the process time. print("Ready to begin next batch") switch.wait_for_press() ##Wait and activate process. print("Process has begun") signal.off() ##Turn on processor. print("Power supply activating") print("Please wait 3 seconds") sleep(3) ##Delay before lowering parts. down.on() ##Lower parts. while process > 10: ##Two while loops are for visual timer. m, s = divmod(process, 60) h, m = divmod(m, 60) time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2) print(time_left + " remaining") counter1 = 0 while counter1 < 10: if GPIO.input(17): signal.on() ##Deactivate processor. sleep(6) ##Delay. down.off() ##Raise parts. print("Process stopped, please reset to continue") quit sys.exit() else: sleep(1) counter1 += 1 process -= 10 while process > 0: m, s = divmod(process, 60) h, m = divmod(m, 60) time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2) print(time_left + " remaining") sleep(1) process -= 1 signal.on() ##Turn off processor. sleep(6) ##Delay. down.off() ##Raise parts. print("Process is complete") print("Please wait 5 seconds before restarting") sleep(5) ##Delay.