Shop OBEX P1 Docs P2 Docs Learn Events
Select... Case help (Limiting a variable from 1 to 12) — Parallax Forums

Select... Case help (Limiting a variable from 1 to 12)

ZeusZeus Posts: 79
edited 2011-02-12 13:01 in BASIC Stamp
Hello,

I am looking for help using "Select Case" statements. I am building an alarm clock and I want to limit the hour input to a range of 1 - 12 hours. A portion of the code is below.

I am also wondering if there is a good reference for "Select Case" statements. The books that I have only cover the basics and my code frequently requires a slightly more complicated solution.

Thanks... Zeus

'Alarm Hour Buttons Block
IF (MenuNum = 0) THEN

SELECT INA & 3
CASE = 3
AlarmHour = 6
CASE = 2
AlarmHour = AlarmHour + 1 // 13
CASE = 1
AlarmHour = AlarmHour - 1 MAX 12
ENDSELECT

ENDIF

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-12 08:40
    How about starting first with an explanation of what you're trying to do? Sure you want to limit the hour input to 1-12, but where is the hour input coming from and what form does it take? You seem to be using at least an UP / DOWN button, but you need to give a better description of what you've got and how it's supposed to behave.
  • ZeusZeus Posts: 79
    edited 2011-02-12 08:54
    Mike Green wrote: »
    How about starting first with an explanation of what you're trying to do? Sure you want to limit the hour input to 1-12, but where is the hour input coming from and what form does it take? You seem to be using at least an UP / DOWN button, but you need to give a better description of what you've got and how it's supposed to behave.

    Mike,

    I'm afraid there is not much more to tell. I find myself stating this project by writing the button inputs portion first. This is my first program, so unfortunately I do not know where it is going yet. You are correct about the up/down user inputs. Basically I want to limit the logic so that the user is unable to enter a bad value. I have not worked out how this will be used within the rest of the program.

    It seems to me that there must be a way to constrain the input with mathematical operators, but I have not had any success at this point with this approach.

    Does this help?

    Zeus
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-02-12 10:05
    Zeus,
    That logic will work if you consider the alarmhour as 0 to 11 instead of 1 to 12.
    SELECT INA & 3 
            CASE = 3                                                                ' both buttons pressed
                  AlarmHour = 5                       
            CASE = 2                                  
                  AlarmHour = AlarmHour + 1 // 12   ' <- needs to be cycle of 12 from 0 to 11
            CASE = 1                              
                  AlarmHour = AlarmHour - 1 MAX 11   ' <- similar, when it goes to -1 (65535 to the BS2), becomes 11
    ENDSELECT
    

    Later on when you compare alarmHour with a value you read from an RTC, do something like this to adjust to a 1 to 12 cycle:
    IF Hour = AlarmHour +1 THEN ....
  • ZeusZeus Posts: 79
    edited 2011-02-12 11:20
    Tracy,

    Thanks for helping me out yet again.

    I do not fully understand your code but the logic seems to make sense. Perhaps I should have asked this question first... What would be the most efficient way (code size wise) to limit the cycle to 1 to 12?

    Zeus
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-02-12 11:36
    The easiest to understand is probably this:
    case 2
      alarmHour = alarmHour + 1
      if alarmHour = 13 then alarmHour = 1
    case 1
      alarmHour = alarmHour - 1
      if alarmHour = 0 then alarmHour = 12
    
  • ZeusZeus Posts: 79
    edited 2011-02-12 13:01
    Thank you Tracy,

    I will compare this to your previous code and see if I can make sense of that as well.

    Thanks again... Zeus
Sign In or Register to comment.