Quantcast
Channel: Python Interrupt Not Working with os.system('clear') - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Python Interrupt Not Working with os.system('clear')

$
0
0

I have a python script running on my Raspberry Pi which uses a button and python interrupts. I'm running into a problem when I use os.system('clear'). Whenever I include that in my code, the interrupt stops working. Here is a sample that shows the button press working for 4 presses and then it stops when os.system('clear') is used. Any idea why?

import os
import time
import sys
from datetime import datetime
import RPi.GPIO as GPIO

prev_time = ""
prev_counter = 0
counter = 0

# Setup GPIO, button, and event
GPIO.setmode(GPIO.BCM)
button_pin = 17 # Connect button to pin 17 and gnd
GPIO.setup(button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(button_pin, GPIO.FALLING, bouncetime=250)

while True:
    try:
        # Display the time every 1 second
        if prev_time != str(datetime.now().strftime('%I:%M:%S %p')):
            prev_time = str(datetime.now().strftime('%I:%M:%S %p'))

            if counter == 5: os.system('clear')

            if prev_counter <> counter:
                print "Button Pressed"
                prev_counter = counter

            print "Counter: " + str(counter)
            print str(datetime.now().strftime('%I:%M:%S %p'))

        # Check for the button press event
        if GPIO.event_detected(button_pin):         
            counter += 1

        time.sleep(0.1)     
    except (KeyboardInterrupt, SystemExit):
        os.system('clear')
        GPIO.cleanup()
        sys.exit(0)

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>