Broken Case statement Logic


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125

    Default Broken Case statement Logic

    I have a program that assigns ASCII values based on a number. In a particular case, the value is '27', I know because I have it Serout showing me this.

    BUT, the Case statement is not catching it.. It should be falling into the "do a different thing" bucket, but it does not..

    In this example, TC1 = 27:

    Select Case TC1
    case 0
    TracID1 = " "

    case TC1 > 0 and TC1 < 27
    Do One thing

    case TC1 >= 27 and TC1 < 37
    Do a different thing
    case 37
    TracID1 = ":"
    case 38
    TracID1 = "."
    case else
    TracID1 = "$"
    End Select


    Since the value being compared is in fact "27" why does it fall all the way down to ELSE, and assign it a value of "$"???

    What's wrong with the logic?

    Tom

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


    Did you find this post helpful? Yes | No

    Default

    Tom Gonser, I beleive the variable to be used in the case statement is to only be declared in the first line of the Select Case. Also the optional directive "IS" is to be used for other than compare functions. I have never seen code written the way you have expressed it in your question. I would have done it this way:

    Select Case TC1

    case 0 'actually equals "0"
    TracID1 = " "

    case is < 27 'catches all instances between "1" and "26"
    Do One thing

    case is <37 'catches all instances between "27" and "36"
    Do a different thing

    case 37
    TracID1 = ":"

    case 38
    TracID1 = "."

    case else
    TracID1 = "$"

    End Select

    You would place the statements in an order as to catch them in order.

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    How odd.. I actually had it working last night, but this AM tried the new suggestion and it broke again. Unfortunately I was an idiot and did not write down what worked. I THINK it was:

    Select Case TC1
    case 0
    TracID1 = " "

    case TC1 < 27 'A-Z
    TracID1 = TC1 + 64

    case TC1 >=27 & TC1 < 37 '0-9
    TracID1 = TC1 + 21

    case 37
    TracID1 = ":"
    case 38
    TracID1 = "."
    case else
    TracID1 = "$"
    End Select

    But it does not work again. As a matter of fact, I tried just testing with a fla out test -

    case TC1 = 27

    and with a value of '27' showing up on my hyperterminal screen, the case logic is not catching it as '27'.... I really don't get that, it is almost like the compiler is not building this code right.

    TG

  4. #4
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    ... actually if I have PBP show me the 'DIG1' of the value that was 27, it is actually 27.7... it needs to be just 27.. How does one round using PBP?

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


    Did you find this post helpful? Yes | No

    Default

    Tom, Where is the value for the variable TC1 comming from?

    Dave Purola,
    N8NTA

  6. #6
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    It is coming from an array of 2 HEX bytes, which are then converted to binary. In the case I am using, the

    the value '44308' is what TRACID_ID is in this example..

    SSD4= (ssmax[1]>>4)
    SSD3= (ssmax[1] & $f)
    SSD2= (ssmax[0]>>4)
    SSD1= (ssmax[0] & $f)

    TracID_D=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1' convert to dec

    TC1=(TracID_D/1600)

    I have attached a spreadsheet that does this also. It uses an "INT" function to keep the result of the TracID_D/1600 rounded to 27. In PBP I need an equivalent function. Otherwise the answer is 27.7 and that messes up the CASE statement.

    IN the excel example, if you remove the INT function, it gives 27.7 also.

    TG
    Attached Files Attached Files

  7. #7
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Default

    WOW!! I just tried setting my value to 27 manually, and it STILL does not work!!

    Check this out:


    TC1=27


    Select Case TC1
    case 0
    TracID1 = " "
    case TC1 < 27 'A-Z
    TracID1 = TC1 + 64
    case TC1 >= 27 '0-9
    TracID1 = TC1 + 21
    case 37
    TracID1 = ":"
    case 38
    TracID1 = "."
    case else
    TracID1 = "$"
    End Select

    The result is "[", when it should be "0"!!! How could I be setting a value of "27" and having this kind of result?

    TG

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


    Did you find this post helpful? Yes | No

    Default

    Tom, If you would please try the code exactly as I have written it in my first reply and tell me what the results are.

    Dave Purola,
    N8NTA

  9. #9
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125


    Did you find this post helpful? Yes | No

    Wink

    The answer was to not use the CASE statement at all.. It looked like it was failing somehow internally. So I moved to:

    If TCX = 0 then TracID1 = " "
    If TCX < 27 then TracID1 = TCX+64
    If TCX >=27 then TracID1 = TCX+21
    If TCX = 37 then TracID1 = ":"
    If TCX = 38 then TracID1 = "."
    if TCX > 38 then TracID1 = "$"

    I think the code is smaller, and best of all it works.

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


    Did you find this post helpful? Yes | No

    Default

    Tom, It only goes to show, There's more than 1 way to skin a cat.......

    Dave Purola,
    N8NTA

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