Launch Box built :) requesting help with limiting rotary encoder value
xanadu
Posts: 3,347


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)
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}
Thank you.

Comments
Andy
Thanks.
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
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]~ {{ ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │ │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │ │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│ │is furnished to do so, subject to the following conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │ │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │ │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ }}