I'm a beginner at programming so bare with me. I'm having some problems with implementing a way to stop my script manually. CTRL+C works perfectly fine in the terminal, but it seems it doesn't always work. I found that out the hard way. See, I set up a script for controlling a LED with a switch, then I set up that script to run on boot which worked fine. The problem is the script is in an infinite loop. It constantly checks if the button has been pressed. Nothing I tried could stop the script and I couldn't even log in to my Pi anymore. I had to format the SD card and reinstall Raspbian. I want to prevent that, so how would I do that?
Here's the code I'm using:
#import of libraries
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#pin 8 input, pin 10 output
GPIO.setup(8, GPIO.IN)
GPIO.setup(10, GPIO.OUT)
while True:
value = GPIO.input(8)
#if the value on pin 10 equals False light up the LED
if value == False:
GPIO.output(10, GPIO.HIGH)
print("Button pressed")
#constantly check for changes on pin 10
while value == False:
value = GPIO.input(8)
#else turn it off
else:
GPIO.output(10, GPIO.LOW)
I tried implementing this:
while True:
input = raw_input("")
if input == "stop":
sys.exit(0)
else:
MAIN
But now both inputs (button and keyboard) are getting mixed up, and the main script doesn't work correctly anymore.
How would I implement a way for the script to check if there has been any keyboard input, while the script is actually waiting for a switch press? Is that even possible? What are other alternatives?