SELECT CASE for ranges of values


Closed Thread
Results 1 to 18 of 18
  1. #1
    caverman's Avatar
    caverman Guest

    Question SELECT CASE for ranges of values

    Sure this may be basic stuff (pun intended) but can you have a range of values for a select case

    Eg Visual basic allows
    SELECT CASE PulseLength
    CASE 165 TO 176
    blah
    CASE blah...

    I want to do something like
    select case PulseLength
    case 165 to 176
    OutValue=0
    case 184 to 192
    OutValue=1
    end select

    But it don't work

    Any ideas?
    Thanks
    Dirk

  2. #2
    caverman's Avatar
    caverman Guest


    Did you find this post helpful? Yes | No

    Unhappy opps, need to add more

    or something like
    select case PulseLength
    case is > 165 and is < 176
    OutValue=0
    case is > 184 and is < 192
    OutValue=1
    end select

    Dirk

  3. #3
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Seems like maybe some 'IF... THEN's might work out better?

  4. #4
    caverman's Avatar
    caverman Guest


    Did you find this post helpful? Yes | No

    Smile Thanks

    Funny how late at night one gets stuck into a Select method without thinking of the obvious easy If solution

  5. #5
    caverman's Avatar
    caverman Guest


    Did you find this post helpful? Yes | No

    Lightbulb But

    But maybe the PICBASIC people could add the CASE x TO y variety that some BASICs have.

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    caverman, Why make a mountain out of a Mole hill. Just do this:

    select case PulseLength
    case is < 165
    you don't want these values because they are below 165 so do something
    case is < 176
    OutValue=0
    case is < 184
    you don't want these values because they are between 176 and 184 so do something
    case is < 192
    OutValue=1
    case else
    you don't want these values because they are between 192 and 255 so do something
    end select

    This is the correct way as far as I can see to do this type of compare situation. If you were to use "IF" statements and try to capture all posibilities the code would look much different.

    Dave Purola,
    N8NTA

  7. #7
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    That looks like a nice clean solution Dave!

  8. #8
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Why not using the statement below?



    IF PulseLength > 165 AND PulseLength < 176 THEN
    OutValue=0
    ELSE
    IF PulseLength > 184 OR PulseLength < 192 THEN OutValue=1
    ENDIF


    ----------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,117


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    Why not using the statement below?

    IF PulseLength > 165 AND PulseLength < 176 THEN
    OutValue=0
    ELSE
    IF PulseLength > 184 OR PulseLength < 192 THEN OutValue=1
    ENDIF

    ----------------------
    Because the above compiles almost 2.5 times longer!

    Ioannis

  10. #10
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    If you meant the size in terms of the words used,

    I had 103 words with "IF" statement and 110 words with "Select Case" statement.

    How did you have 2.5 times more words?
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  11. #11
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    sayzer, You are right, it maybe smaller but you have only captured half of the cases. What are you going to do about the rest of the cases ie: < 165, > 176 but < 184, and > 192?

    Dave Purola,
    N8NTA

  12. #12


    Did you find this post helpful? Yes | No

    Default case is... confusing?

    Would you have to jump out to a common goto though, after each < completes its routine? otherwise it'll execute all the ones that apply (i.e. - if your number is 130, and you have a case <140, case <150, case <160, case <170, etc. ALL apply after case <130), or is the nature of the select case such that the first one that applies ONLY is executed?

    --------------Picster----------------

  13. #13
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,117


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    If you meant the size in terms of the words used,

    I had 103 words with "IF" statement and 110 words with "Select Case" statement.

    How did you have 2.5 times more words?
    Hi!

    With the F877 chip I got 43 and 114 for the two codes.

    What chip did you try?

    Generally you have to keep the if statements as "thin" as possible. An AND can be replaced by one more IF resulting in less words.

    Ioannis

  14. #14
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    picster, Yes you are correct. The first true instance in the case statement flow is the final test before the end select .

    Dave Purola,
    N8NTA

  15. #15
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Cool

    Hi Ioannis,

    I compiled the code with F628A.

    Pls check this post at http://www.picbasic.co.uk/forum/showthread.php?t=3548

    From PIC to PIC "words used" changes as you may guess.


    -------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,117


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    Hi Ioannis,

    I compiled the code with F628A.

    Pls check this post at http://www.picbasic.co.uk/forum/showthread.php?t=3548

    From PIC to PIC "words used" changes as you may guess.
    -------------
    Yes it changes indeed. But it is a little bizzare to have such differences for devices so similar (628-877).

    I put only the modefs include. Did you put any other? Other defines?

    Ioannis

  17. #17
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Question

    I did not include any modifier file.
    Just the code itself.

    Meanwhile, lets take your "select case" and compile it with
    16F628A
    16F877
    12C509A

    The result is:

    16F628A = 95 words
    16F8777 = 110 words
    12C509A = 123 words

    How interesting!
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  18. #18
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,117


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer
    ...
    The result is:

    16F628A = 95 words
    16F8777 = 110 words
    12C509A = 123 words

    How interesting!
    Especially for the smallest part that has the largest output!!!

    Ioannis

Similar Threads

  1. Using Nokia LCD
    By BobP in forum mel PIC BASIC Pro
    Replies: 300
    Last Post: - 3rd May 2018, 04:47
  2. Sony SIRC IR Issue
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th August 2015, 08:10
  3. Write Onewire data toa I2C memory / read ASCI
    By Eugeniu in forum mel PIC BASIC Pro
    Replies: 67
    Last Post: - 16th November 2008, 19:19
  4. Crystalfontz LCD
    By jman12 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 9th February 2007, 15:04
  5. Interrupt/timer not really interrupting...
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd May 2005, 22:05

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts