Help understanding ELSE command
I am monitoring buttons. When I press the button "N" it turns on the output as expected. However when I release the button the output stays on. So I added an ELSE command but it won't compile. Am I using the wrong command?
When I press the "X" button it turns off the output as expected.
When I press the "X" button it turns off the output as expected.
repeat
case key_pressed
"X" : ' STOP key pressed
outa[RLY_UP..RLY_DN] := 0 ' turn off table motor relays
"N" : ' MINUS key pressed
outa[RLY_UP] := 1 ' turn on table up relay
vfd.SetRowCol(2, 0) ' position cursor on VFD to second line
vfd.PrintStr(string("Table UP")) ' display message on VFD
else
outa[RLY_UP] := 0

Comments
You are looking for OTHER.
[FONT=monospace]repeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] := 0 ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] := 1 ' turn on table up relay vfd.SetRowCol(2, 0) ' position cursor on VFD to second line vfd.PrintStr(string("Table UP")) ' display message on VFD OTHER: outa[RLY_UP] := 0[/FONT]Tried your idea but it tells me that OTHER has to be at the end of the method. I tried indenting different ways and get other errors where it expects instructions or variables. When I placed it at the end it still didn't turn off after releasing the key.
I'll show more of the CASE loop. Sorry I didn't include it all previously.
Something I am missing here...
repeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] ~ ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] := 1 ' turn on table up relay vfd.SetRowCol(2, 0) ' position cursor on VFD to second line vfd.PrintStr(string("Table UP")) ' display message on VFD other: outa[RLY_UP] ~ "P" : ' PLUS key pressed outa[RLY_DN] := 1 ' turn on table down relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table DOWN")) ' display message on VFD "R" : ' PROG button pressed i := 0 ' set buffer pointer to 0 repeat 6 ' repeat 6 times to fill buffer with key stokes buff[i] := kp.getKey ' waiting for next key press i := i + 1 ' increment buffer pointer after each key press "S" : ' START key pressedrepeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] ~ ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] := 1 ' turn on table up relay vfd.SetRowCol(2, 0) ' position cursor on VFD to second line vfd.PrintStr(string("Table UP")) ' display message on VFD other: outa[RLY_UP] ~ "P" : ' PLUS key pressed outa[RLY_DN] := 1 ' turn on table down relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table DOWN")) ' display message on VFD "R" : ' PROG button pressed i := 0 ' set buffer pointer to 0 repeat 6 ' repeat 6 times to fill buffer with key stokes buff[i] := kp.getKey ' waiting for next key press i := i + 1 ' increment buffer pointer after each key press "S" : ' START key pressedAnd this...
repeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] ~ ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] := 1 ' turn on table up relay vfd.SetRowCol(2, 0) ' position cursor on VFD to second line vfd.PrintStr(string("Table UP")) ' display message on VFD "P" : ' PLUS key pressed outa[RLY_DN] := 1 ' turn on table down relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table DOWN")) ' display message on VFD "R" : ' PROG button pressed i := 0 ' set buffer pointer to 0 repeat 6 ' repeat 6 times to fill buffer with key stokes buff[i] := kp.getKey ' waiting for next key press i := i + 1 ' increment buffer pointer after each key press "S" : ' START key pressed other: outa[RLY_UP] ~As for "Expected an instruction or variable". When it gives you this error it will highlight something. It is telling you this highlighted thing is not defined. It may be an needed object or variable. Make sure your variables are defined globally in a VAR block or locally after a "|" after your PUB/PRI declaration. Or you may need the "vfd" object declared in a OBJ block.
Here's the code...
con _clkmode = xtal1 + pll16x _clkfreq = 80_000_000 MS_001 = _clkfreq / 1_000 RS = 9 ' VFD pin assignments RW = 10 E = 11 DBLow = 12 DBHigh = 15 PWR = 20 ' power sense voltage divider input EE_DEVICE_ADDR = $A0 ' EEPROM assigments EE_BASE_ADDR = $9000 RLY_UP = 22 ' table relay outputs. up counts down RLY_DN = 23 con #1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS ' PST formmatting control ' 0.003515185598214 ' multiplier for inches / pulse obj term : "fullduplexserialdp" ' for terminal output level : "jm_grayenc2" ' 2-bit, graycode encoder kp : "Keypad_encoder" ' keypad driver object P0 - P7 vfd : "LCD_Nx2" ' VFD driver simp : "Simple_Numbers_plus" ' used for formating decimal places i2c : "Basic_I2C_Driver_1" ' EEPROM driver var byte key_pressed long key_stack_space[50] pub main | newlevel, oldlevel, Pressed_Key term.start(31, 30, %0000, 115_200) ' start terminal for test pause(2000) ' wait 2 seconds term.tx(CLS) ' clear terminal screen cognew(Keys, @key_stack_space) ' launch new cog for keypad interface dira[PWR] := 0 ' set PWR as input outa[RLY_UP..RLY_DN] ~ ' initialize relay outputs to off i2c.initialize(28) ' initialize I2C object level.init(16, true, 0, 1707, i2c.ReadLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0))) ' detented encoder on p16/p17 ' initialize counter input object and get starting value from EEPROM vfd.Init( E, RS, RW, DBHigh, DBLow ) ' initialize VFD driver kp.start(4, 4, 0, 4, @table) ' start keypad driver pause(1) vfd.clear ' clear vfd screen term.str(string("Table Controller", 13)) ' print to PST for debug term.str(string("Version 0.09")) ' print to PST for debug vfd.PrintStr(string("Table Controller")) ' print to VFD vfd.SetRowCol(2, 0) ' set position to next row on VFD vfd.PrintStr(string("Version 0.09")) ' print to VFD pause(4000) ' wait 4 seconds vfd.clear ' clear VFD display term.tx(CLS) ' clear PST screen for debug term.tx(LF) ' set position to next line in PST term.str(string("Press PROG for Menu")) ' print to PST screen for debug vfd.SetRowCol(2, 0) ' set position to second row on VFD vfd.PrintStr(string("Press PROG for Menu")) ' print to VFD pause(500) ' wait 1/2 second repeat ' Main program loop oldlevel := newlevel ' assign newlevel to oldlevel term.tx(HOME) ' set PST screen to home position vfd.SetRowCol(0, 0) ' set position on VFD to first line and home vfd.PrintStr(string("Position: ")) ' print string to VFD term.str(string("Position: ")) ' print string to PST screen for debug vfd.SetPos(16) ' set postion on VFD for inches character vfd.PrintChr($22) ' print inches character on VFD newlevel *= 3515 ' multiply count input by scale factor to convert to inches newlevel /= 100 ' divide new count value to scale for display term.decdp(newlevel, 4) ' print inches value to PST screen with 4 decimal places vfd.SetPos(10) ' set position on VFD to display inches value vfd.PrintStr(simp.decf(newlevel, 4)) ' print inches value to VFD with 4 decimal places repeat if ina[PWR] == 0 ' Looks at P20 for low to indicate loss of power ' i2c.WriteLong(i2c#BOOTPIN, EE_DEVICE_ADDR, constant(EE_BASE_ADDR + $0), newlevel) abort ' if PWR low detected then write current value to EEPROM then stop Pressed_Key := kp.getkey ' read key press assign that value to local pressed_key variable if Pressed_Key > 0 ' if that variable is greater than 0 key_pressed := Pressed_Key ' then assign that value to key_pressed global variable newlevel := level.read ' read count input and assign to newlevel until (newlevel <> oldlevel) ' go to main loop and display values whenever they differ pub Keys | buff[6], i dira[RLY_UP..RLY_DN] ~~ ' set relay ports as outputs repeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] ~ ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] := 1 ' turn on table up relay vfd.SetRowCol(2, 0) ' position cursor on VFD to second line vfd.PrintStr(string("Table UP")) ' display message on VFD "P" : ' PLUS key pressed outa[RLY_DN] := 1 ' turn on table down relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table DOWN")) ' display message on VFD { "R" : ' PROG button pressed i := 0 ' set buffer pointer to 0 repeat 6 ' repeat 6 times to fill buffer with key stokes buff[i] := kp.getKey ' waiting for next key press i := i + 1 ' increment buffer pointer after each key press "S" : ' START key pressed } other : outa[RLY_UP] ~ pub pause(ms) | t t := cnt repeat ms waitcnt(t += MS_001) dat ' keypad translation table table byte "1", "2", "3", "S" byte "4", "5", "6", "R" byte "7", "8", "9", "E" byte "N", "0", "P", "X" ' Keypad layout ' ' 1 2 3 Start ' 4 5 6 Prog ' 7 8 9 Set ' - 0 + StopI think the problem is with this line: "if Pressed_Key > 0"
Assuming Pressed_Key is set to 0 when nothing is pressed (I'm not sure this is how it works), the cog that controls your IOs never gets the update that nothing is being pressed.
The line of code that makes this now work is: repeat until kp.getrow == 0
repeat case key_pressed "X" : ' STOP key pressed outa[RLY_UP..RLY_DN] ~ ' turn off table motor relays "N" : ' MINUS key pressed outa[RLY_UP] ~~ ' turn on table up relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table UP")) ' display message on VFD repeat until kp.getrow == 0 ' monitor key release outa[RLY_UP] ~ ' turn off table up relay "P" : ' PLUS key pressed outa[RLY_DN] ~~ ' turn on table down relay ' vfd.SetRowCol(2, 0) ' position cursor on VFD to second line ' vfd.PrintStr(string("Table DOWN")) ' display message on VFD repeat until kp.getrow == 0 ' monitor key release outa[RLY_DN] ~ ' turn off table down relay { "R" : ' PROG button pressed i := 0 ' set buffer pointer to 0 repeat 6 ' repeat 6 times to fill buffer with key stokes buff[i] := kp.getKey ' waiting for next key press i := i + 1 ' increment buffer pointer after each key press "S" : ' START key pressed } other : outa[RLY_UP..RLY_DN] ~