ericfmyers
Posts: 3
Joined: Wed Jul 09, 2014 10:44 pm

barcode string matching

Wed Jul 09, 2014 11:14 pm

Hello, everyone. I'm new to the Raspberry Pi and to Python. For my first project I became a little ambitious and set up a verification system.

How it works:
A barcode scanner attached to the Raspberry Pi scans one barcode (on the outside of a box) and another scanner scans another barcode (maybe on the inside of the box. Packing list or invoice). If the two are a match, a green LED flashes and the motor running the conveyor continues to run. If the two barcodes do not match then the motor stops and a red light comes on. A button is also attached to the Raspberry Pi to reset and start again.

The code that I created is works, it just seems too simple. I don't want it to just work, I want it to be correct. There's got to be like six more pages of code that I'm missing, right?

Code: Select all

# 4 Line Switch Off/On
# 17 Error light
# 23 Good Scan
# 18 Reset Button

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.OUT)
GPIO.output(4, True)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, True)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, True)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
import time

GPIO.wait_for_edge(18, GPIO.RISING)



while True:
    GPIO.output(4, False)
    GPIO.output(17, True)
    GPIO.output(23, True)

    if raw_input("1st Scan:") == raw_input("2nd Scan:"):
GPIO.output(23, False)
time.sleep(.3)
GPIO.output(23, True)
        continue

    else:
        print("NO MATCH")
GPIO.output(4, True)
GPIO.output(17, False)
GPIO.wait_for_edge(18, GPIO.RISING)
        continue
Thanks,
Eric

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: barcode string matching

Wed Jul 09, 2014 11:43 pm

ericfmyers wrote:The code that I created is works, it just seems too simple. I don't want it to just work, I want it to be correct. There's got to be like six more pages of code that I'm missing, right?
Heheh http://xkcd.com/353/ :D

If it works, then it works! When code is so simple, there's not much that can go wrong.
The only thing I'd tweak would be to get rid of the unnecessary 'continue' statements.
And IMHO

Code: Select all

if raw_input("1st Scan:") == raw_input("2nd Scan:"):
looks stylistically unusual - a more normal way to do it might be

Code: Select all

barcode1 = raw_input("1st Scan:")
barcode2 = raw_input("2nd Scan:")
if barcode1 == barcode2:
as this then allows you to do "other things" with the barcode values (print them to the screen, log them to a file, whatever).

Edit: And I see you're using the internal pull-up resistor for the button, but also waiting for a rising edge, which I believe means it'll wait for the button to be released rather than being pressed. But if that's the desired behaviour, then fine!

ericfmyers
Posts: 3
Joined: Wed Jul 09, 2014 10:44 pm

Re: barcode string matching

Thu Jul 10, 2014 2:04 am

Yes, Python is sort of amazing. Even when I mess up, it tells me what is wrong. It's like PHP and HTML5 have been mocking me for years.

I got rid of the continues. I'm not sure why I added them in the first place.

I took your suggestion and created the barcode1 and 2 variables. I'm don't know what else I would do with them, but you're right, it's nice to have that option if it's needed.

The reset button is also the start button. I changed the lines to GPIO.FALLING. I'm not sure if it matters.

You can see that the program doesn't really have an exit. If I CTRL+C when the Error LED is on, it stays on until I start the program again. Is there some way to turn all of the outputs off if the program is terminated?

Thanks,
Eric

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: barcode string matching

Thu Jul 10, 2014 2:33 am

ericfmyers wrote:You can see that the program doesn't really have an exit. If I CTRL+C when the Error LED is on, it stays on until I start the program again. Is there some way to turn all of the outputs off if the program is terminated?
http://raspi.tv/2013/rpi-gpio-basics-3- ... ct-your-pi

ericfmyers
Posts: 3
Joined: Wed Jul 09, 2014 10:44 pm

Re: barcode string matching

Thu Jul 10, 2014 10:04 pm

I figured it out. Thank you for the link.

Return to “Python”