I have this script that is for a rotary encoder and is as such:
How would I write it such that is a function so that I can call it from other fucntions??
Code: Select all
from RPi import GPIO from time import sleep clk = 17 dt = 18 #lm1 = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #GPIO.setup(lm1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) counter = 0 clkLastState = GPIO.input(clk) try: while True: clkState = GPIO.input(clk) dtState = GPIO.input(dt) if clkState != clkLastState: if dtState != clkState: counter += 1 else: counter -= 1 #if lm1 == 1 : #counter = 0 print (counter) clkLastState = clkState finally: GPIO.cleanup()