"Select Case" Help


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Default "Select Case" Help

    Is there a way to isolate a range of numbers in a case statement?

    I've used this:

    Select Case Voltage
    Case is < 512
    Expression...
    End Select

    But what if I want numbers less than 512 AND greater than 256??!

    Tried this...didn't work:
    Case is < 512 and is > 256

    Tried this...didn't work:
    Case 256 to 512

    Tried this...didn't work:
    Case 256:512

    Any ideas how to do this? Basically I'm trying to type the code below in a more elegant way. I'm using IF-THEN's at the moment but since there is no ELSEIF command I have 4 separate blocks. I would like one block so I can use an ELSE statement (or in this situation a CASE ELSE). If I use an ELSE statement in the fourth block that ELSE statement will ALWAYS operate if the fourth block is false (even if one of the first 3 blocks is true)...NOT GOOD CODING!

    PLEASE HELP!

    If (Voltage <=256) and (Voltage > 0) Then
    PORTC = %0001
    PAUSE 100
    Endif
    If (Voltage <=512) and (Voltage > 256) Then
    PORTC = %0010
    PAUSE 100
    ENDif
    If (Voltage <= 768) And (Voltage > 512) Then
    PORTC = %0100
    Pause 100
    endif
    If (Voltage <= 1023) and (Voltage > 768) Then
    PORTC = %1000
    Pause 100
    ENDif

    How can I do this with Case Statements??

    Thanks in advance!
    -Brian

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I know this doesn't answer your question.
    But perhaps all you need is ...
    Code:
        PORTC = DCD Voltage.HighByte
        PAUSE 100
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Code:
    <font color="#000080"><i>
    
    </i></font>Temp <font color="#000080"><b>VAR WORD
    
    </b></font>Temp = <font color="#FF0000">512 </font>- voltage <font color="#000080"><i>' Voltage 
    
    </i><b>SELECT CASE </b></font>Temp
    
        <font color="#000080"><b>CASE IS </b></font>&lt; <font color="#FF0000">256 </font><font color="#000080"><i>' Voltage is greater than 256.
        
    </i><b>END SELECT
    </b></font>
    Last edited by sayzer; - 24th November 2009 at 08:55. Reason: typo
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    bmsherma, Try this...

    SELECT CASE VOLTAGE

    CASE < 257
    PORTC = %0001

    CASE < 513
    PORTC = %0010

    CASE < 769
    PORTC = %0100

    CASE < 1024
    PORTC = %1000

    END SELECT
    Pause 100

    I use this construct when determining groups of numbers. You start with the smallest and move up to the largest.

    Dave Purola,
    N8NTA

  5. #5
    Join Date
    Nov 2009
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Thanks

    Thanks everyone for the advice.

    Dave P, your idea seems to work. However what if I want to isolate the low range OR the high range. I'm essentially trying to put logical operators in my case statement.

    Case is < 256 OR is > 768
    PORTC = %0001

    Case Else
    PORTC = %1000

    Can I write that first Case statement with the OR term?

    Or would I have to do this:

    Case is < 256
    PORTC = %0001
    Case is > 768
    PORTC = %0001
    Case else
    PORTC = %1000

    Is there no way to use logical operators in Case Statements in PICBasicPRO??!

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


    Did you find this post helpful? Yes | No

    Default

    bmsherma, The second way you have it is the way I would do the construct..

    Dave Purola,
    N8NTA

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by bmsherma View Post
    Is there no way to use logical operators in Case Statements in PICBasicPRO??!
    NO!, logical operators are NOT allowed in Case statements.
    <br>
    DT

  8. #8
    Join Date
    Nov 2009
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Thanks

    Thanks everyone for the help!!

    This forum is GREAT

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Wink

    Hi,

    With a very, very very little Trickery ... I think it makes it.

    Code:
    IF ( Voltage.8 ^ Voltage.9 ) THEN 
    
       PORTC = 1
    
    ELSE
    
       PORTC = 8
    
    ENDIF
    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,611


    Did you find this post helpful? Yes | No

    Wink

    Or ... With Select Case

    Code:
    Voltage = Voltage.Highbyte
    
    SELECT CASE Voltage
    
        Case 1,2
    
           PORTC = 1
    
        Case Else
    
           PORTC = 8
    
    END SELECT
    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Members who have read this thread : 1

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