I am a novice Python programmer, trying to write a Python script to run a barcode scanning station (Raspberry Pi 4, RPiOS, Python 3.7.3). I want to scan a certain number of items, and when complete I want to scan a different barcode to process the input. From there either it will send a GPIO signal if batch count is met, or terminate the program outright if the count is too high or low.
I have been successful with what I've written, with one exception: when a second barcode is scanned to process the input, it needs to be scanned TWICE to trigger the rest of the script. I'm stumped as to why this is, and am sure this is something simple I overlooked.
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
import time
good = 38
GPIO.setmode(GPIO.BOARD)
GPIO.setup(good,GPIO.OUT)
GPIO.output(good,GPIO.HIGH)
try:
def batch_order():
batch = input("Enter the number of parts on the order: ")
barcode1 = input("Scan MASTER barcode: ")
print("Begin scanning barcodes: ")
count = 0
while True:
if input() == barcode1:
count+= 1
print("Part count: %s"%(str(count)))
elif input() != barcode1:
time.sleep(0.2)
if batch == str(count):
print("Order complete! Now printing shipping label...")
time.sleep(0.2)
GPIO.output(good,False)
time.sleep(15)
GPIO.output(good,True)
break
else:
print("Order incorrect, %s of %s parts scanned."%(str(count), batch))
time.sleep(2)
break
batch_order()
except KeyboardInterrupt:
GPIO.cleanup()
print("QUIT")
GPIO.cleanup()
All I want to do is eliminate the double scan required to terminate the program at elif input() != barcode1:
. Any help would be greatly appreciated, thank you!