I am using LS s40-6-1000ZT(H) 1000 p/r rotary encoder for my project with raspberry pi 3,but raspberry not working properly as per encoder specification(i.e not counting event properly).What could be the issue? Any solution??
from RPi import GPIO
from time import sleep
import tkinter as tk
a=22
b=23
GPIO.setmode(GPIO.BCM)
GPIO.setup(a,GPIO.IN)
GPIO.setup(b,GPIO.IN)
root=tk.Tk()
clkLastState=GPIO.input(a)
def call():
global clkLastState
global counter
clkState=GPIO.input(a)
if clkState != clkLastState:
dtState=GPIO.input(b)
if dtState != clkState:
counter +=1
else:
counter -=1
text=str(counter/2)
print (text[0:-2])
var.set(text)
clkLastState=clkState
def my_callback(channel):
call()
counter=0
clkLastState=GPIO.input(a)
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)
print (counter)
var=tk.IntVar()
L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
L1.pack()
# var.set(counter)
# root.after(500,main)
root.geometry("100x100")
#GPIO.add_event_detect(22,GPIO.BOTH,callback=my_callback)
GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback,debouncetime=6)
root.mainloop()
#var=tk.IntVar()
#L1=tk.Label(root,bg="orange",fg="red",textvariable=var)
#L1.pack()
#GPIO.add_event_detect(23,GPIO.BOTH,callback=my_callback)
GPIO.cleanup()