Hi,
I found that i cannot interrupt my code with ^C or the Stop button in Pycharm, if python is occupied with reading the Infrared Sensor. If i try to use this Keyboardinterrupt, the programm will keep running for a few more seconds, before exiting with exit code 143, and no further information. This is bad, because then the programm is not stopped nicely, and other functions go wrong.
I made a short example code to show my problem, shown below.
When the wait time is much longer, say 5 seconds, than you can be sure to interrupt during the waiting, which then works fine with exit code 0. But that is not possible for my programm, i want to be able to interrupt the analogRead.
import time
import grovepi
sensor = 0
grovepi.pinMode(sensor, "INPUT")
sensor_value = 0
try:
while True:
i = 0
sensor_value_total = 0
while i < 100:
# Read sensor value
sensor_value = grovepi.analogRead(sensor)
sensor_value_total += sensor_value
i += 1
# Durchschnittswert ueber die letzten i Messwerte
sensor_value_average = sensor_value_total / i
print(sensor_value_average)
time.sleep(0.1)
except KeyboardInterrupt:
pass
print("nicely quit")
Now an example output:
ssh://pi@ipadres/usr/bin/python3.7 -u /home/pi/Documents/Python/Testprogram.py
292.1
291.4
291.4
292.1
^C293.1
292.0
^Cnicely quit
Process finished with exit code 0
Where you can see a ^C 2 seconds before I quit, this one thus didnt quit the programm. Later I tried again, and appearently timed well such that it quit with exit code 0
another example:
ssh://pi@ipadres/usr/bin/python3.7 -u /home/pi/Documents/Python/Testprogram.py
292.14
291.98
292.14
^C291.52
291.98
291.85
292.18
292.16
291.88
292.04
Process finished with exit code 143
Exit code 143 approximately 7 seconds after ^C
I really hope someone can help me