Shop OBEX P1 Docs P2 Docs Learn Events
Scrolling a variable from 0 - 59 — Parallax Forums

Scrolling a variable from 0 - 59

ZeusZeus Posts: 79
edited 2011-01-29 15:22 in BASIC Stamp
Hello Everyone (1st. post),

I have a simple problem that I can not see my way around.

I am making an alarm clock and in trying to prevent the user from entering a value greater then 59 minutes I have limited the variable (defined as a byte) to between 0 and 59. I have two input buttons that scroll the value up and down.

The problem that is that when the "value" is "1" and I attempt to subtract 1 from it it jumps to "59" and not "0" as expected. However when the value is "59" and 1 is added to it the value "0" is delivered.

My code is below, can anyone help me with the logic or point out my mistake? Also, if there is a more efficient way to accomplish my goal I would be interested in knowing that as well.

Thanks,

Zeus



IF (IN1 = 1) THEN
Value = Value + 1
ELSEIF (IN0 = 1) THEN
Value = Value - 1
ENDIF

IF(IN0 = 1) AND (IN1 = 1)THEN
Value = 0
ENDIF

IF (Value > 59) THEN
Value = 0
ENDIF

IF (IN0 = 1) AND (Value = 0) THEN
Value = 59
ENDIF

Comments

  • $WMc%$WMc% Posts: 1,884
    edited 2011-01-29 12:02
    I think your last "IF" is forcing the 59
    '
    Have you tried "Value =0" instead of 59?
    '
    Its better if you post the whole code with REM statments.
    '
    I'm not sure if your using active low of high inputs? This would be somthing to add in your REM statments
  • ZeusZeus Posts: 79
    edited 2011-01-29 12:18
    That does solve my original problem however it introduces another. When I set it equal to zero you can scroll up to 59 and then it loops back to zero but when you scroll down is stops scrolling at zero.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-01-29 12:33
    I think $WMc% is right about that last IF statement. Another way to do it encapsulates the 0-59 cycle in one case structure. You have 4 conditions null, count up mod 60, count down mod 60, and reset to zero.
    value VAR BYTE
    SELECT INA & 3    ' mask off bits in1 and in0, possibilities are 00,01,10 and 11.
         CASE =3    ' reset
             value = 0
         CASE =2   ' count up mod 60 using // operator
             value = value + 1 // 60
         CASE =1    ' count down mod 60, return to 59 when value becomes 255
             value = value - 1 MAX 59
    ENDSELECT
    
  • ZeusZeus Posts: 79
    edited 2011-01-29 13:04
    Thanks Tracy,

    This is my first program so your code is a bit beyond me right now. I will investigate "Case Structures" and see if I can figure it out.

    Is it possible to solve this with IF/THEN statements/logic?

    Zeus
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-01-29 13:46
    Welcome to the stamp and to the forums, by the way!
    >>Is it possible to solve this with IF/THEN statements/logic?

    Sure. Here is one way, using if and elseif. I left the math operators:
    x // 60 returns the remainder after division by 60. So when the count hits 60, it automatically returns to zero.
    x MAX 59 returns either the value of x, when x is less than or equal to 59, or 59 if x is greater than 59. It happens that when you subtract 1 from 0 on the Stamp, in a byte variable, the value is 255 (not -1). So the MAX operator causes the expression to roll over, from zero back up to 59.

    value VAR BYTE
    IF IN1=1 AND IN0=1 THEN
      value = 0
    ELSEIF IN1=1 AND IN0=0 THEN
      value = value + 1 // 60
    ELSEIF IN1=0 AND IN0=1 THEN
      value = value - 1 MAX 59
    ENDIF
    

    Here is another way where the program reads the state of both buttons into one variable and then looks at the possible states. That is getting close to what the SELECT-CASE statement is doing.
    value VAR BYTE
    state VAR NIB
    state = INA & 3
    IF state=3 THEN
      value = 0
    ELSEIF state=2 THEN
      value = value + 1 // 60
    ELSEIF state=1 THEN
      value = value - 1 MAX 59
    ENDIF
    

    You may need a debouncing code, or a timer, something to keep the above snippet from executing out of control in a loop.
  • ZeusZeus Posts: 79
    edited 2011-01-29 15:22
    Tracy,

    Thank you very much for your help.

    Your first bit of code above is more where I am at skill wise but I will look into the "Select Case" regardless as my button code has the potential to be too large if I do not find a way to write it more efficiently.

    Oddly enough I was looking into mathematical operators the other day regarding this problem but failed to see how it (//) applied.

    Thank you again,

    Zeus
Sign In or Register to comment.