Keyes Rotary Encoder Code Problem

josefvs

New Member
Hi All,

I have purchased a Keyes Rotary Encoder, the following code was used on a 28X2, a toggle switch is used to set the count jumps in steps of 1 or 10. The unit is designed for a boat autohelm so the numbers dialled by the encoder is 1 to 359.

The problem is that the whole thing works fine while rotating the encoder slowly, on higher rotation speed he number do not go up or down.

Any idea why?

Thanks


init:
symbol lega=pina.0
symbol legb=pina.1
symbol sethdg=w0
symbol cw=b2
symbol cw1=b3
symbol lasta=b4
symbol mult=b5
pause 500
serout B.4,N2400,(254,1)
pause 50
serout B.4,N2400,(254,128)
serout B.4,N2400,("SET")
serout B.4,N2400,(254,132)
serout B.4,N2400,("000")

sethdg=0
mult=1

main:
if pinb.3=1 then
mult=10
else mult=1
end if
if lega=1 and legb=1 then
cw=1
lasta=1
end if
if lega=0 and legb=0 then
cw1=1
lasta=0
end if

if lega=1 and legb=0 then
cw=0
lasta=1
end if
if lega=0 and legb=1 then
cw1=0
lasta=0
end if

if lasta=lega then goto main

if cw=1 or cw1=1 then
sethdg=sethdg-mult
if sethdg>360 then
sethdg=359
end if
if sethdg<10 then gosub dig1
if sethdg>9 and sethdg<100 then gosub dig2
if sethdg>99 then gosub dig3
end if

if cw=0 or cw1=0 then
sethdg=sethdg+mult
if sethdg>359 then
sethdg=0
end if
if sethdg<10 then gosub dig1
if sethdg>9 and sethdg<100 then gosub dig2
if sethdg>99 then gosub dig3
end if

goto main

dig1:
serout B.4,N2400,(254,132)
serout B.4,N2400,("00")
serout B.4,N2400,(254,134)
serout B.4,N2400,(#sethdg)
return

dig2:
serout B.4,N2400,(254,132)
serout B.4,N2400,("0")
serout B.4,N2400,(254,133)
serout B.4,N2400,(#sethdg)
return

dig3:
serout B.4,N2400,(254,132)
serout B.4,N2400,(#sethdg)
return
 

rossko57

Senior Member
The problem is that the whole thing works fine while rotating the encoder slowly, on higher rotation speed he number do not go up or down.
All about timing then.
A start could be to up the Picaxe clock rate, so that it can do more in the time available.

One obvious difficulty is that your code will spend most of its time doing SEROUT, and will miss input changes while it is doing that. I'd look into ways of using X2 background transmit and/or higher baud rates

The if/then code reads the input pins several times - you could get unexpected results if the pins change during the process, Ensure consistency by reading the pins into a variable once, and do the decisions based on that variable until next time the input is polled.
 

hippy

Ex-Staff (retired)
Also have a look at other posts on the forum relating to "rotary encoder" and "quadrature encoder".

The maximum rotation speed which can be used depends on how quickly the code can loop and how frequently it checks the inputs. Increasing the operating speed is the first step but minimising and optimising the code will also help.
 
Top