minimal fixes to temendously terrible logic on the part of the original author

This commit is contained in:
Brennen Raimer
2019-01-18 13:47:05 -05:00
parent 1f77452e11
commit 0ae49b9afe
3 changed files with 21 additions and 22 deletions
+11 -11
View File
@@ -1,7 +1,9 @@
# Written By Johnathan Cintron and Devlyn Courtier for the HCCC Library
import sys
import time, MySQLdb
import MySQLdb
from datetime import datetime
from time import sleep
import RPi.GPIO as GPIO
ultraSonicConst = "95"
@@ -14,7 +16,7 @@ GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
# Setup GPIO in and out
# Setup GPIO in and out
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
@@ -28,14 +30,13 @@ cursor = db.cursor()
try:
while True:
curr_date = time.strftime("%Y-%m-%d")
curr_time = time.strftime("%H:%M:%S")
curr_date = datetime.now()
GPIO.output(TRIG, False)
time.sleep(0.5)
sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.00001)
sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
@@ -49,7 +50,7 @@ try:
distance = pulse_duration * 17150
distance = round(distance, 2)
if distance < 120:
count = count + 1
@@ -62,11 +63,10 @@ try:
# Prepare SQL query to INSERT a record into the database.
#if any( [int(time.strftime("%H")) == 8, int(time.strftime("%H")) == 15, int(time.strftime("%H")) == 22] ) and (int(time.strftime("%M")) == 0):
if any( [time.strftime("%M") == "00", int(time.strftime("%M")) == 10, int(time.strftime("%M")) == 20, int(time.strftime("%M")) == 30, int(time.strftime("%M")) == 40, int(time.strftime("%M")) == 50]) and (time.strftime("%S") == "00"):
if (curr_date.minute % 10 == 0) and (curr_date.second == 0):
try:
# Create SQL Query
sql = "INSERT INTO ULTRASTATS (date, time, gatecount) VALUES ('%s', '%s', '%d')" % (curr_date, curr_time, count)
sql = "INSERT INTO ULTRASTATS (datetime, gatecount) VALUES ('%s', '%d')" % (curr_date.isoformat(' '), count)
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
@@ -75,7 +75,7 @@ try:
# Rollback in case there is any error
db.rollback()
time.sleep(0.5)
sleep(0.5)
except KeyboardInterrupt:
db.close()
print("\nCtrl-C pressed cleaning up GPIO")