import RPi.GPIO as GPIO import time from smbus import SMBus bus = SMBus(1) an0 = 0 led1 = 23 led2 = 22 led3 = 25 color_pw = 20 br1 = 0 br2 = 0 br3 = 0 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(led1,GPIO.OUT) GPIO.setup(led2,GPIO.OUT) GPIO.setup(led3,GPIO.OUT) GPIO.setup(color_pw,GPIO.OUT) GPIO.output(led1,GPIO.LOW) GPIO.output(led2,GPIO.LOW) GPIO.output(led3,GPIO.LOW) GPIO.output(color_pw,GPIO.HIGH) def read_ain(i): global bus bus.write_byte(0x48, i) bus.read_byte(0x48) return bus.read_byte(0x48) print "CTRL+C = STOP program" try: while True: an0 = read_ain(1) val = 160 - an0 # PULL UP on 3.3V ledv = val + 70 # > 150 turn LED ON (50%) print str(an0) print "-" bus.write_byte_data(0x48, 0x40, ledv) # analog LED if an0 > 156 and br1 > 3: GPIO.output(led1,GPIO.HIGH) br1 = 0 else: GPIO.output(led1,GPIO.LOW) if an0 < 153 and br2 > 3: GPIO.output(led2,GPIO.HIGH) br2 = 0 else: GPIO.output(led2,GPIO.LOW) if an0 == 156 and br3 > 3: GPIO.output(led3,GPIO.HIGH) br3 = 0 else: GPIO.output(led3,GPIO.LOW) # samples for better precision if an0 == 156 or an0 == 155: br3 = br3 + 1 if an0 < 153: br2 = br2 + 1 if an0 > 156: br1 = br1 + 1 except KeyboardInterrupt: GPIO.cleanup() # must be executed before program stop