To use this file copy and paste this:
// #URL-lib "http://pin1.org/forthlib/flb/Examples/rotary.fth" into BV Terminal 3
or here to download.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Rotary encoders 1
// This version does not use interrupts and culd be easily
// used as a library for other projects.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb"
// CONSTANTS:
// define the three wires used for the encoder
25 constant line1
26 constant line2
6 constant pushb
//
variable oldRa
variable oldRb
variable detent
variable temp
Full Contents of File
25  constant  line1
26  constant  line2
6    constant  pushb
variable  oldRa
variable  oldRb
variable  detent
variable  temp
: r-set
        line1  io-in
        line2  io-in
        pushb  io-in         
;
: ra@?         
        line1  p@  1+  oldRa  @  1  and  xor
;       
: rb@?         
        line2  p@  1+  oldRb  @  1  and  xor
;       
: Ra@     
        ra@? 
        if
                1  ms  ra@?
                if           
                        line1  p@  1+  oldRa  !
                then                                 
        then                     
        oldRa  @  1-
;
: Rb@
        rb@? 
        if
                1  ms  rb@?
                if           
                        line2  p@  1+  oldRb  !
                then                                 
        then                     
        oldRb  @  1-
;
: Rc@pushb  p@  ;
: r-rot
        ra@  rb@  2dup  xor   
        if   
                drop             
                detent  @   
                ?dup
                if           
                        0  detent  !           
                        xor         
                        0<  if  1  else  -1  then 
                        -1           
                else   
                        drop  0           
                then                                   
        else
                detent  @    0=   
                if
                        +    1+           
                        detent  !       
                        0                       
                else
                        2drop  0         
                then               
        then
;
   
: r-test
        r-set
        0  temp  !
        begin
                r-rot
                if    temp  +!  temp  @  .  then
                key?
        until
;