Shop OBEX P1 Docs P2 Docs Learn Events
Launch Box built :) requesting help with limiting rotary encoder value — Parallax Forums

Launch Box built :) requesting help with limiting rotary encoder value

xanaduxanadu Posts: 3,347
edited 2012-06-14 18:36 in Propeller 1
cbox.jpg
<-wireless keyboard for laptop on robot, not launch box.
cboxinside.jpg
<-old pic, all wired up now. all in one unit though, only the battery sits in the bottom of case

I only want the rotary encoder to have 2 positions for now. So it can only equal two different numbers. Then I can use it as a rotary selector for two options and it can switch between them relatively fast.

I am on page 63 of the PE book for reference. I can make example code that works and limits the VAR to two numbers using a push button.

It seems as though I cannot limit the variable with #> or #< inside of my program using the encoder. So I'm guessing that it needs to be done in the object?

The object is Quad Decoder - http://obex.parallax.com/objects/666/

Here is my current code:
{{

Launch Box Project

}}

CON

 _clkmode = xtal1 + pll16x  '333 gigawatt crystal 
 _xinfreq = 5_000_000       'recalibrate decalibrator 

 ENCODER_PIN = 12           'first encoder pin (12+13) 

 XBT = 17                   'XBee DIN                                                  
 XBR = 16                   'XBee DOUT

OBJ

 quad  :  "QuadDecoder"
  lcd  :  "SparkFun_serial_lcd"
 xbee  :  "XBee_Object_2" 

VAR

 byte encval  'encoder variable 

PUB main

 'init VARs
  encval := 1     'rot enc initial value   

 'init used pins
  dira[23] := 1   'rot enc red LED
  dira[22] := 1   'rot enc green LED
  dira[21] := 1   'rot enc blue LED
  dira[18] := 0   'rot enc push button

  dira[5]  := 0   'arm switch
  dira[6]  := 0   'red push button

  dira[1]  := 1   'ARM sw status led
  outa[1]  := 0   'yellow

  dira[2]  := 1   'PWR sw status led (no switch logic)
  outa[2]  := 1   'green 

  dira[3]  := 1   'BTN status led
  outa[3]  := 0   'red

                  'let's get this LCD party started
  lcd.init(7,9600,2,20)
  lcd.displayOn               
  lcd.cursor(0)
  lcd.backlight(true)
  lcd.clrln(0)
  lcd.clrln(1)

                  'XBee after party begins now
  xbee.start(XBR, XBT, %0000, 9_600)                            
  waitcnt(clkfreq * 1 + cnt)
  xbee.at_init
  xbee.str(string("Launch Box Online"))  
  
                  'begin enc program to take over the planet 
  quad.start(ENCODER_PIN, @encval)                      

  go

PUB go  
        
 repeat
   outa[1] := ina[5]             'send arm sw to yellow LED
   outa[3] := ina[6]             'send red but to red LED
   
   waitcnt(clkfreq/10 + cnt)
   
  
   lcd.dec(encval) 
  
{{
&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
&#9474;&#61469;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570; TERMS OF USE: MIT License &#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61469;&#9474;                                                            
&#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
&#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    &#9474; 
&#9474;files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    &#9474;
&#9474;modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software&#9474;
&#9474;is furnished to do so, subject to the following conditions:                                                                   &#9474;
&#9474;                                                                                                                              &#9474;
&#9474;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.&#9474;
&#9474;                                                                                                                              &#9474;
&#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          &#9474;
&#9474;WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         &#9474;
&#9474;COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   &#9474;
&#9474;ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         &#9474;
&#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
}}

Thank you.
980 x 735 - 284K
1024 x 768 - 103K

Comments

  • AribaAriba Posts: 2,690
    edited 2012-06-14 12:40
    You can try it with:
      lcd.dec(encval & 1)
    
    if it not matters when both turn directions toggle the value.

    Andy
  • xanaduxanadu Posts: 3,347
    edited 2012-06-14 12:52
    Perfecto! That works great. The encoder is incrementing in 5's so I used a slightly higher number. If you wouldn't mind, what function is that called? Is that something I missed on variables, or something else?

    Thanks.
  • kwinnkwinn Posts: 8,697
    edited 2012-06-14 16:46
    That's the logical "and" function. When you and two values together the result has a 1 bit set only where there was a 1 bit in both values. For instance if you and the binary values 0101 and 0011 the result would be 0001.
  • AribaAriba Posts: 2,690
    edited 2012-06-14 16:58
    It's a logical AND with 1, so this isolates the lowest bit of the variable encval. The lowest bit changes between 0 and 1 every increment or decrement (think of: even, odd, even odd...).
    You can also isolate other bits with & 2 the result changes every second step between 0 and 2. With & 4 you get 0 or 4 every fourth step, and so on.
    The variable encval still counts up and down in the full range and will always be overwritten by the QuadDecoder object, so if you need the toggling value in a variable you need to copy it into a new variable:
    myencval := encval & 1

    Andy
  • xanaduxanadu Posts: 3,347
    edited 2012-06-14 18:36
    Awesome explanation thanks guys. I seem to have a much better understanding of this now.

    I just updated my code for the box. It's sending a character to an XBee in loop back and waiting for the response, depending on the position of the encoder knob it either beeps or blinks, or both. So now I can range test it in my RC airplane, sort of a heart beat signal if you will.


    This box project is way too much fun lol.
    {{
    
    Launch Box Project
    
    }}
    
    CON
    
     _clkmode = xtal1 + pll16x  '333 gigawatt crystal 
     _xinfreq = 5_000_000       'recalibrate decalibrator 
    
     ENCODER_PIN = 12           'first encoder pin (12+13) 
    
     XBT = 17                   'XBee DIN                                                  
     XBR = 16                   'XBee DOUT
    
    OBJ
    
     quad  :  "QuadDecoder"
      lcd  :  "SparkFun_serial_lcd"
     xbee  :  "XBee_Object_2" 
    
    VAR
    
     byte encval      'encoder variable
     byte stbpst      'for LCD condition flags
     byte stbnst      '"
     long stack1[100] 'stack space
     long c
     long beepfrq       ' 
                                    
    PUB inits
    
     beepfrq := 210_367
    
     beep             'reboot warning  
     beep2
     beep
     beep2 
    
     'init VARs
      encval  := 1     'rot enc initial value
      stbpst  := 15    'LCD condition flag
      
    
     'init used pins
      dira[23] := 1    'rot enc red LED
      dira[22] := 1    'rot enc green LED
      dira[21] := 1    'rot enc blue LED
      dira[18] := 0    'rot enc push button
                     
    ' dira[5]  := 0   'arm switch
      dira[6]  := 0    'red push button
    
    ' dira[1]  := 1   'ARM sw status led
    ' outa[1]  := 0   'yellow off
    
      dira[2]  := 1    'PWR sw status led (no switch logic)
      outa[2]  := 1    'green on 
    
    ' dira[3]  := 1   'BTN status led
    ' outa[3]  := 0   'red off
    
    ' PIN 27 = Speaker
    
                      'let's get this LCD party started
      lcd.init(7,9600,2,20)
      lcd.displayOn               
      lcd.cursor(0)
      lcd.backlight(true)
      lcd.clrln(1)
      lcd.clrln(0)
     
      lcd.str(string("Initializing...."))
    
                      'XBee after party begins now
      xbee.start(XBR, XBT, 00, 9_600)                            
      waitcnt(clkfreq * 1 + cnt)
      xbee.at_init
    ' xbee.str(string("Launch Box Online"))  
      beep2 
                      'begin enc program to take over the planet 
      quad.start(ENCODER_PIN, @encval)                      
    
      runp
      
    PUB runp
    
     lcd.gotoxy(0, 1)                'program started
     lcd.str(string("--modem online--"))
     cognew(pingxbee, @stack1[50])
    
     xbee.rxflush                    ' clear trash from xbee
      repeat
        
        c := xbee.rxtime(250)
        
         case c     
           "w", "W":
             lcd.gotoxy(14, 0)                                            
             lcd.str(string("RX"))       
             stbpst := 15            'reset LCD condition flag
             outa[21] := 1           'rot enc blue LED
             outa[23] := 1           'rot enc red LED
             outa[22] := 1           'rot enc green LED
             beep 
       
                                     'update LCD with mode
       if ((stbnst := encval & 10) <> stbpst)       
          if (stbnst)
            beepfrq := 210_367
            lcd.gotoxy(0, 0)
            lcd.str(string("Ready           "))
             outa[21] := 0           'rot enc blue LED
             outa[23] := 0           'rot enc red LED
             outa[22] := 1           'rot enc green LED
              cognew(readyp, @stack1[0])
              cogstop(4)
          else
            beepfrq := 0
            lcd.gotoxy(0, 0)
            lcd.str(string("Standby         "))
             outa[21] := 1           'rot enc blue LED
             outa[23] := 0           'rot enc red LED
             outa[22] := 0           'rot enc green LED
              cognew(standbyp, @stack1[30])
              cogstop(5)
          stbpst := stbnst
    
       if ina[18] == 1               'test spk
           beep2    
    
      
    PUB readyp
    
     dira[1] := 1 
     outa[1] := 1
    
      repeat
        dira[3] := 1
        outa[3] := 1
        waitcnt(clkfreq / 5 + cnt)             
        outa[3] := 0
        waitcnt(clkfreq / 50 + cnt)
        outa[3] := 1
        waitcnt(clkfreq / 5 + cnt)
        outa[3] := 0
        waitcnt(clkfreq * 1 + cnt)    
      
    
    PUB standbyp
    
      repeat       
        dira[1] := 1 
        outa[1] := 1
        waitcnt(clkfreq / 500 + cnt)
        outa[1] := 0
        waitcnt(clkfreq * 1 + cnt)
    
    PUB pingxbee
    
     ' xbee.start(XBR, XBT, 00, 9_600)                            
     ' waitcnt(clkfreq * 1 + cnt)
     ' xbee.at_init
      
     repeat
       
       xbee.str(string("w"))
       waitcnt(clkfreq * 3 + cnt)         
    
    PUB Beep 
    
     ctra[30..26] := 100               
     ctra[5..0] := 27                     
     frqa := beepfrq                      
     dira[27]~~                         
     waitcnt(clkfreq / 10 + cnt)
     dira[27]~
    
    PUB Beep2 
    
     ctra[30..26] := 100               
     ctra[5..0] := 27                     
     frqa := 150_367                       
     dira[27]~~                         
     waitcnt(clkfreq / 10 + cnt)
     dira[27]~  
    
        
    
    
    
    {{
    &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    &#9474;&#61469;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570; TERMS OF USE: MIT License &#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61573;&#61570;&#61469;&#9474;                                                            
    &#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
    &#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    &#9474; 
    &#9474;files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    &#9474;
    &#9474;modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software&#9474;
    &#9474;is furnished to do so, subject to the following conditions:                                                                   &#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.&#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          &#9474;
    &#9474;WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         &#9474;
    &#9474;COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   &#9474;
    &#9474;ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         &#9474;
    &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
    }}
    
Sign In or Register to comment.