Shop OBEX P1 Docs P2 Docs Learn Events
Help understanding ELSE command — Parallax Forums

Help understanding ELSE command

Don MDon M Posts: 1,653
edited 2011-07-13 19:53 in Propeller 1
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.
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

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-13 13:17
    ELSE commands are for IF statements.
    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]
    
  • Don MDon M Posts: 1,653
    edited 2011-07-13 13:34
    Bob,

    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 pressed
    
  • Don MDon M Posts: 1,653
    edited 2011-07-13 13:36
    Tried 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
          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 pressed
    

    And 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] ~ 
    
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-13 13:57
    Yes, the OTHER needs to be the last statement in the CASE.

    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.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-13 14:00
    Post your full program. that will help expedite this process. I don't think the problem is with the CASE statement here.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 14:03
    OTHER was the word highlighted that it didn't like. But that was when I did not have it at the end.

    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  +  Stop 
    
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-13 14:18
    Well it appears you shouldn't be getting any more errors.
    I 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.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 14:34
    So most likely what I need to do is come up with some different logic to decode my key press codes. I like the CASE statement idea as it makes it look neat and easy to follow.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-13 14:37
    The CASE statement is definitely the way to go in that case. You just need the no-key-pressed information to be passed to the cog with the CASE. It may be as simple as removing that IF statement ("if Pressed_Key > 0").
  • Don MDon M Posts: 1,653
    edited 2011-07-13 15:26
    No that didn't help either. Need to rethink this.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 15:54
    Ok JonnyMac thanks. Your code works using PST and works the same as what I had using my keypad. If I press "N" the LED for RLY_UP comes on and stays on. What I am looking for is the LED to go off when I release the "N" button. If I press "P" the RLY_DN LED should come on and then go off when I release the button.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-07-13 17:12
    You're going to need to save the last key pressed so that you can undo that press when there is change on the next scan. Explaining what you want -- in detail -- is a far more effective way to get assistance in these forums than posting busted code.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 18:42
    Jon- Thanks for your contribution. As always it's very much appreciated. I thought I had mentioned what I was looking to do. And as far as busted code? Well that was the purpose of posting it- to see what I was doing wrong.
  • Don MDon M Posts: 1,653
    edited 2011-07-13 19:53
    So I managed to get this working with some help of a good friend. In the keypad_encoder object I made a new public method called getrow. What it does is watch for the keys to be released and returns a value when the row is released. So I watch for this value to equal 0 (when you take your finger off the key) and when it does it turns off the relay.

    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] ~   
    
Sign In or Register to comment.