Shop OBEX P1 Docs P2 Docs Learn Events
IND vs. INA — Parallax Forums

IND vs. INA

ZeusZeus Posts: 79
edited 2012-07-18 15:17 in BASIC Stamp
To All,

Can someone please explain the following code in very basic terms? Specifically I am struggling with why my code delivers the inverse of what I intended, Up is Down & Down is Up. I suspect that this has something to do with my lack of understanding of both the Select Case as well as the "INA & 3" statement. This is just a portion of my code below. I was using IND and everything was working fine but switch button inputs and this necessitated the use of "INA".
Buttons_Sub:                          ' Start of Sub Routine.

' Menu Number Buttons Block
  IF (IN2 = 0) AND (IN3 = 0) THEN   ' Start of "If" statement which adresses both buttons being presses.
    MenuNum = 0                       ' If above statement is true "MenuNum" is set equal to zero.
    ELSEIF (IN2 = 0) THEN            ' Begining of "If" Statement which adds 1 from "MenuNum" variable.
    MenuNum = MenuNum + 1             ' If above condition is true, adds 1 from "MenuNum" variable.
    ELSEIF (IN3 = 0) THEN            ' Begining of "If" Statement which subtracts 1 from "MenuNum" variable.
    MenuNum = MenuNum - 1             ' If above condition is true, subtracts 1 from "MenuNum" variable.
  ENDIF                               ' End of "IF" statement.

' Alarm Minutes Buttons Block

IF (MenuNum = 1) THEN
  SELECT INA & 3                      ' Possibilities are 00, 01, 10 and 11.
    CASE = 0
      AlmMins = 0                     ' Reset to Zero

    CASE = 1
      AlmMins = AlmMins + 1 // 60     ' Count up mod 60 using // operator
    ENDIF

    CASE = 2
      AlmMins = AlmMins - 1 MAX 59    ' Count down mod 60, return to 59 when value becomes 255
    ENDIF

  ENDSELECT
ENDIF

The inputs are active low...

IN0 Scrolls up the menu
IN1 Scrolls down the menu
IN2 Add One to the Minutes value
IN3 Subtracts One from the Minutes value
IN4 is a Snooze button, but I don't believe this is the cause of my problem.

Thanks in advance,

Zeus

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-17 16:30
    INA & %0001 is the same as IN0
    INA & %0010 is the same as IN1 although you can't test it against 1, you have to use %0010
    INA & %0100 is the same as IN2 although you can't test it against 1, you have to use %0100
    INA & %1000 is the same as IN3 although you can't test it against 1, you have to use %1000

    INA & 3 is the same as INA & %0011. If you use it in a SELECT,
    CASE 0 is IN0 = 0 and IN1 = 0
    CASE 1 is IN0 = 1 and IN1 = 0
    CASE 2 is IN0 = 0 and IN1 = 1
    CASE 3 is IN0 = 1 and IN1 = 1
  • ZeusZeus Posts: 79
    edited 2012-07-17 17:27
    Thank you Mike,

    So if I understand you correctly given that my inputs are active low, my code should actually read...
    ' Menu Number Buttons Block
      IF (IN2 = 0) AND (IN3 = 0) THEN     ' Start of "If" statement which adresses both buttons being presses.
        MenuNum = 0                       ' If above statement is true "MenuNum" is set equal to zero.
        ELSEIF (IN2 = 0) THEN             ' Begining of "If" Statement which adds 1 from "MenuNum" variable.
        MenuNum = MenuNum + 1             ' If above condition is true, adds 1 from "MenuNum" variable.
        ELSEIF (IN3 = 0) THEN             ' Begining of "If" Statement which subtracts 1 from "MenuNum" variable.
        MenuNum = MenuNum - 1             ' If above condition is true, subtracts 1 from "MenuNum" variable.
      ENDIF                               ' End of "IF" statement.
    
    ' Alarm Minutes Buttons Block
    IF (MenuNum = 1) THEN
      SELECT INA & 3                      ' Possibilities are 00, 01, 10 and 11.
        CASE = 3
          AlmMins = 0                     ' Reset to Zero
    
        CASE = 2
          AlmMins = AlmMins + 1 // 60     ' Count up mod 60 using // operator
        ENDIF
    
        CASE = 1
          AlmMins = AlmMins - 1 MAX 59    ' Count down mod 60, return to 59 when value becomes 255
        ENDIF
      ENDSELECT
    ENDIF
    

    This has corrected some of the issue however the IF THEN statement is still producing inverse results, what am I missing?

    Zeus
  • Mike GMike G Posts: 2,702
    edited 2012-07-17 17:30
    IN0 Scrolls up the menu
    IN1 Scrolls down the menu
    IN2 Add One to the Minutes value
    IN3 Subtracts One from the Minutes value
    IN4 is a Snooze button, but I don't believe this is the cause of my problem.

    No, in your code example

    IN2 = 0 scrolls the menu up
    IN3 = 0 scrolls the menu down

    IN0 and IN1 control the AlmMins

    The code snippet provided will briefly contain expected values. If menuNum = 1 and the buttons are released AlmMins = 0. Unless you reset MenuNum somewhere.

    You really should poll the INA register and place the results in a "VAR" buttons. Then it is real easy to parse the encoded buttons variable and make a decision. Even better, press two buttons and go into alarm mode. Press up and down buttons to make selections. Press a single button to enter the selection.
  • ZeusZeus Posts: 79
    edited 2012-07-18 15:17
    Mike,

    Mike G is correct I screwed up and wrote down the previous input configuration. Could I ask you to write the logic you provided in your last post based around the correct configuration below?

    IN0 Add One to the Minutes value
    IN1 Subtracts One from the Minutes value
    IN2 Scrolls up the menu
    IN3 Scrolls down the menu
    IN4 is a Snooze button

    I know that this request might sound ridiculous but I learn from examples and I want to compare the two as the INA statements still do not make sense to me.

    Zeus
Sign In or Register to comment.