Shop OBEX P1 Docs P2 Docs Learn Events
Code question — Parallax Forums

Code question

Don MDon M Posts: 1,653
edited 2012-12-10 16:08 in Propeller 1
Lets say I'm parsing through some word size data. The data falls within these ranges:

0108 - 010F
0110 - 0117
0130 - 0137

To look at all data, as I'm doing now I use this statement:
if (data & $0100) == $0100 
  something happens

My question is how do I structure my if statment to look for just any one or combination of the ranges I mentioned above?

Thanks.
Don

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-12-10 09:18
    Like this...
      if(data => $0108 AND data =< $010F)
        'do something
    

    I'd use constants for $0108 and $010F.
  • kwinnkwinn Posts: 8,697
    edited 2012-12-10 09:31
    For multiple ranges using a case statement may be faster and is easier to follow than multiple if/elseif statements.
  • RaymanRayman Posts: 14,826
    edited 2012-12-10 10:27
    yes, I'd use case too...
  • Mike GMike G Posts: 2,702
    edited 2012-12-10 11:14
    Agreed, CASE is much cleaner.
  • lardomlardom Posts: 1,659
    edited 2012-12-10 11:25
    This is an example of the case statement.
    PUB Main : result | PWLevel
    
         ctra[30..26] := %01000                  ' "POS detector"
         ctra[5..0] := 6                        
         frqa := 1                                     
    
         dira[6] := outa[6] := 1               
         waitcnt(clkfreq/100_000 + cnt)          ' Wait for circuit to charge        
    
         phsa~                                   
         dira[6]~                               ' Pin to input starts measurement     
    
         waitcnt(clkfreq/60 + cnt)               ' Seems to be enough time to get value
     
         PWLevel := (phsa - 624) #> 0  
    
         [B]Case [/B]   PWLevel                         
            0..3210       : PWLevel :=  0        
            3211..6420    : PWLevel :=  1
            6421..9630    : PWLevel :=  2        
            9631..12840   : PWLevel :=  3
            12841..16050  : PWLevel :=  4
            16051..19260  : PWLevel :=  5
            19261..22470  : PWLevel :=  6
            22471..25680  : PWLevel :=  7
            25681..28890  : PWLevel :=  8
            28891..32100  : PWLevel :=  9
            32101..35310  : PWLevel :=  10
            35311..38520  : PWLevel :=  11
            38521..41730  : PWLevel :=  12
            41731..44940  : PWLevel :=  13
            44941..48150  : PWLevel :=  14
            48151..51360  : PWLevel :=  15
            51361..54570  : PWLevel :=  16
            54571..57780  : PWLevel :=  17
            57781..60990  : PWLevel :=  18
            60991..64200  : PWLevel :=  19
    
         result := PWLevel
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-10 12:46
    Larry, Do you need those exact numbers?

    It looks like this would work
    PWLevel /= 3211
    

    For all but a few extremes of your ranges.
  • lardomlardom Posts: 1,659
    edited 2012-12-10 13:10
    @Duane Degn, It must be an improvement over what I was doing. What does "/=" do? I was toying with ranges using a breadboard potentiometer.
  • Don MDon M Posts: 1,653
    edited 2012-12-10 13:24
    Yes I'm familiar with case statements. What I was looking for was the structure of an IF statement and how to handle a range within.

    @Mike- that was what I was looking for and it works. Thanks.

    Here's is what I came up with. It works and if there are suggestions to make it cleaner and simpler I'm all ears...
    con
    
      A1 = $0108                                                         
      A2 = $010F
    
      B1 = $0110                                           
      B2 = $0117
    
      C1 = $0130                                          
      C2 = $0137
    
    pub
    
      case mode
        $0:                                                 
          data1 := A1  
          data2 := C2
        $1:                                                 
          data1 := A1  
          data2 := A2
        $2:                                                 
          data1 := C1  
          data2 := C2
        $3:                                                 
          data1 := B1 
          data2 := B2      
    
      if(data => data1 AND d =< data2)                      
              do something   
    
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2012-12-10 13:32
    This would be easier with an array:
    DAT
     minval word A1, A1, C1, B1
     maxval word C2, A2, C2, B2
    
    PUB ...
      ...
      if( data => minval[ mode ] and data =< maxval[ mode ]
           do something
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-10 13:33
    lardom wrote: »
    @Duane Degn, It must be an improvement over what I was doing. What does "/=" do? I was toying with ranges using a breadboard potentiometer.

    Sorry, I forgot this was Don's thread. You were just giving an example of case.

    The "/=" is used when the result is part of the equation. Say you want to x to equal x divided by 2 you could use:
    x := x / 2
    

    or even shorter:
    x /= 2
    

    I just thought your data in your case statement looked like a linear relationship. It's always nice when one can find practical applications for math.
    Sorry for the detour Don.
  • Don MDon M Posts: 1,653
    edited 2012-12-10 16:08
    MagIO2 wrote: »
    This would be easier with an array:
    DAT
     minval word A1, A1, C1, B1
     maxval word C2, A2, C2, B2
    
    PUB ...
      ...
      if( data => minval[ mode ] and data =< maxval[ mode ]
           do something
    

    Thanks. I like the simplicity. Works well.
Sign In or Register to comment.