Scrolling a variable from 0 - 59
Zeus
Posts: 79
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
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
'
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
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
>>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.
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.
You may need a debouncing code, or a timer, something to keep the above snippet from executing out of control in a loop.
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