Im working on a game for my RPi3 B running raspbian, whenever I run this py script from the command line it runs once and stops, I would like it to stay open and keep running as it waits for a knock sensor to be knocked.. I am new to python if that helps. When the script detects a knock sensor it sends a value (Strike 1) to my sqlite db. I then retrieve that data using php and ajax into my browser running from my local apache server. It all works if I run the py script first in idle as it stays open but not from the command line..
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sqlite3
KnockPin = 12
print ("Welcome to Bases Loaded")
def setstr():
global strikes
strikes = 0
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(KnockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def knock(ev=None):
with sqlite3.connect('/var/www/html/basesLoaded.db') as conn:
global strikes
strikes += 1
if strikes ==0:
pass
elif strikes == 1:
conn.execute("UPDATE bl set STRIKE = 1 WHERE ID =1");
print ("Strike 1 :", conn.total_changes);
elif strikes == 2:
conn.execute("UPDATE bl set STRIKE = 2 WHERE ID=1");
print ("Strike 2 :", conn.total_changes);
elif strikes == 3:
conn.execute("UPDATE bl set STRIKE = 0 WHERE ID=1");
print ("Strike 3 Your Out :", conn.total_changes);
setstr()
def register_callbacks():
GPIO.add_event_detect(KnockPin, GPIO.FALLING, callback=knock, bouncetime=2500)
if __name__ == '__main__':
try:
setup()
register_callbacks()
except KeyboardInterrupt:
destroy()