Demo entry 3548674

python

   

Submitted by anonymous on Jan 16, 2016 at 05:11
Language: Python. Code size: 1.3 kB.

#!/usr/bin/env python

import RPi.GPIO
import time
import termios

GREEN_LED_PIN = 19
RED_LED_PIN = 26

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)

RPi.GPIO.setup(GREEN_LED_PIN, RPi.GPIO.OUT)
RPi.GPIO.setup(RED_LED_PIN, RPi.GPIO.OUT)

tty = open('/dev/tty1')

barcodeDatabase = {'42080503': False, '420805039374889877724128716345': True}

while True:
    termios.tcflush(tty.fileno(), termios.TCIOFLUSH)
    print 'Please scan barcode...'
    barcode = tty.readline().rstrip()

    print 'Barcode scanned: ', barcode

    if barcodeDatabase.has_key(barcode):
        if barcodeDatabase[barcode] == True:
            print 'Recyclable.'

            RPi.GPIO.output(GREEN_LED_PIN, True)
            time.sleep(3)
            RPi.GPIO.output(GREEN_LED_PIN, False)
        else:
            print 'Not recyclable.'

            RPi.GPIO.output(RED_LED_PIN, True)
            time.sleep(3)
            RPi.GPIO.output(RED_LED_PIN, False)
    else:
        print 'Barcode is not in the database.'

        for i in range(0, 5):
            RPi.GPIO.output(GREEN_LED_PIN, True)
            RPi.GPIO.output(RED_LED_PIN, True)
            time.sleep(0.5)
            RPi.GPIO.output(GREEN_LED_PIN, False)
            RPi.GPIO.output(RED_LED_PIN, False)
            time.sleep(0.5)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).