Shop OBEX P1 Docs P2 Docs Learn Events
"Limit Minimum" syntax help — Parallax Forums

"Limit Minimum" syntax help

lardomlardom Posts: 1,659
edited 2014-08-11 09:04 in Propeller 1
I'm building a method that modifies a variable by multiplying it with a 'percentage' value. I want minimum to maximum limits of 100 to 4000.
if percentage <> 0 
  PW := percentage * PW / 100
'PW" is initialized with 1_200. The conditional statement prevents PW from clearing to 0, a fatal error. It would be great if I could put the limits in the CON block.

Comments

  • Sir GawainSir Gawain Posts: 32
    edited 2014-08-11 07:26
    I'm not sure exactly what you want...
    Something like this?
    CON
    
    PWMAX = 4000
    PWMIN = 100
    
    VAR
    
    long PW
    long percentage
    
    PUB Main
    
    if percentage <> 0
      'after every new entry to set PW you add:
      PW <#= PWMAX     'limit maximum     see Propeller manual p. 155...
      PW #>= PWMIN     'limit minimum
      'you are validating the input before using it in your code
      PW := percentage * PW / 100
    
  • ratronicratronic Posts: 1,451
    edited 2014-08-11 07:58
    Larry if you want to set a lower limit for a variable you can do this -
    pw #>= 1000
    

    To limit to a maximum you can do this -
    pw <#= 2000
    

    And to limit within a range you can do this -
    pw := 1000 #> pw  <# 2000
    

    Edit: You can also do this to limit min/max = pw #> 1000, pw <# 2000
  • lardomlardom Posts: 1,659
    edited 2014-08-11 08:38
    I'm building a basic Spin encoder object. If the object can handle at least 50 negative edges per second it'll be a good start. I'm trying to keep the code as small as possible.
    PUB Main | percentage, PW, CurrentSpeed
        PW := 1_200               
        repeat
          CurrentSpeed := T4.Get_Ticks
          percentage := CurrentSpeed*100/Start_Speed
          if percentage <> 0 
            PW := percentage*PW/100                                                          
    

    Start_Speed = 1600 clocks per negative edge. The CurrentSpeed is compared to 1600 clocks and PW is modified by the same percentage that they don't match. PW can be any number from 1 to 4000. If I could configure the limits as constants I could eliminate at least two lines of code.
  • lardomlardom Posts: 1,659
    edited 2014-08-11 08:50
    ratronic, just saw your post. Looks good. I'll try it, thanks.
  • ratronicratronic Posts: 1,451
    edited 2014-08-11 09:04
    You can put your upper and lower limit amounts as CONstants. Also I am correcting the Edit in my post above to show the proper min/max.
Sign In or Register to comment.