Shop OBEX P1 Docs P2 Docs Learn Events
Having trouble with understanding gray encoder code. — Parallax Forums

Having trouble with understanding gray encoder code.

I've been trying to get this code to work but am having trouble understanding it.

I've put together some example code and will do my best to try and explain it. I'm using a 3 legged rotary encoder switch with a push button. I've set the initialization to be from 1 to 512.

Then for my loop I only have 3 choices so I change the min/max of the encoder object to 1 to 3. I read the encoder in a loop using a case statement to make decisions. the main loop appears to work as expected only counting from 1 to 3 then stops and lets you go backwards from 3 to 1.

If I'm on case 1 and push the button I change the range of the encoder object to be the full range that I initialized it with. I can turn the encoder and it counts to 512 and stops as I expect it to. I can then rotate backwards and count down stopping at 1. I press the button again to exit that case. I then advance to case 2 and press the button. Here I change the range to 1 to 100. I then rotate the knob and it counts up to 100 and will continue on up to 512. Even though the Min / Max show the range to be 1 to 100.

Case 3 also works incorrectly as I set its range from 1 to 10 but it too will continue on past 10.

What is strange to me is that with the exit of each routine I set the Min/Max back to 1 to 3 and that seems to work.

Appreciate any and all help and comments.
Thanks.
PUB Main | idx, idxadd, idxatt, idxv, i, i1, i2, i3, i4, i5                                       

  encdr.init(20, 1, 1, 512, 1, 22)                      ' detented encoder on P20 and pushbutton on P22 
                                                        ' (base, hasdetent, lo, hi, preset, pbpin)
                                                        ' Start encoder with range of 1 to 512 preset to 1  
  term.start(31, 30, %0000, 115_200)                    ' start terminal using FDS
  pause(1000)

  term.str(string("Encoder test", 13, 13))
  
  encdr.chgrange(1, 3, 1)                               ' (lo, hi, preset)
                                                        ' Change range of encoder to 1 to 6 preset to 1

  term.str(string(13, "Encoder: "))
  term.dec(encdr.read)
  term.str(string(" Lo: "))
  term.dec(encdr.readlo)
  term.str(string(" Hi: "))
  term.dec(encdr.readhi)
  term.tx(13)

  i  := 1
  i1 := 2
  i2 := 3
  i3 := 4
  i4 := 5
  i5 := 6

  idxadd := 1
  idxatt := 1
  idxv := 0

   repeat

      idx := encdr.read
   
      case idx 
      
        1 :

          if i == idx
            repeat 1                                            
              term.str(string("Case 1", 13))
              term.str(string("Encoder: "))
              term.dec(encdr.read)
              term.str(string(" Lo: "))
              term.dec(encdr.readlo)
              term.str(string(" Hi: "))
              term.dec(encdr.readhi)
              term.tx(13)
              i := 9
              i1 := 2
          
          if encdr.ispushed == 1
            release
            encdr.chgrange(1, 512, 1)                   ' Change encoder to full range of 512 preset to 1
            
            repeat
              idx := (encdr.read // 513)
              if idx <> idxadd 
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                idxadd := idx
              if encdr.ispushed == 1
                release
                encdr.chgrange(1, 3, 1)
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                quit
                  
        2:                                              
          if i1 == idx
            repeat 1                                            
              term.str(string("Case 2", 13))
              term.str(string("Encoder: "))
              term.dec(encdr.read)
              term.str(string(" Lo: "))
              term.dec(encdr.readlo)
              term.str(string(" Hi: "))
              term.dec(encdr.readhi)
              term.tx(13)
              i := 1
              i1 := 0
              i2 :=3
          
          if encdr.ispushed == 1
            release
            encdr.chgrange(1, 100, 1)                   
            
            repeat
              idx := (encdr.read // 101)
              if idx <> idxatt
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                idxatt := idx
              if encdr.ispushed == 1
                release
                encdr.chgrange(1, 3, 1)
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                quit

        3:                                              
          if i2 == idx
            repeat 1                                            
              term.str(string("Case 3", 13))
              term.str(string("Encoder: "))
              term.dec(encdr.read)
              term.str(string(" Lo: "))
              term.dec(encdr.readlo)
              term.str(string(" Hi: "))
              term.dec(encdr.readhi)
              term.tx(13)
              i := 1
              i1 := 2
              i2 := 0
          
          if encdr.ispushed == 1
            release
            encdr.chgrange(1, 10, 1)                   
            
            repeat
              idx := (encdr.read // 11)
              if idx <> idxv
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                idxv := idx
              if encdr.ispushed == 1
                release
                encdr.chgrange(1, 2, 1)
                term.str(string("Encoder: "))
                term.dec(encdr.read)
                term.str(string(" Lo: "))
                term.dec(encdr.readlo)
                term.str(string(" Hi: "))
                term.dec(encdr.readhi)
                term.tx(13)
                quit                

pub  release   'wait for button release

  repeat until encdr.ispushed == 0
            
pub pause(ms) | t

  t := cnt
  repeat ms
    waitcnt(t += MS_001)

Comments

  • That object is pretty old, so I've attached my latest object that handles an encoder and button.

    If you want to change the encoder range mid program, you can. What you need to do is get the current reading and save it, then reinitialize to the new range and preset the value for that range. Use the .set() method to preset the value of the encoder (for example, when you come back to a given range).
  • Don MDon M Posts: 1,652
    Thank you Jon.
Sign In or Register to comment.