limiting to "no less than" zero


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323

    Default limiting to "no less than" zero

    Greetings all,

    Too many interests and hobbies have kept me away from electronics for a while, but I'm back again and trying to re-learn what little I knew about PICs....

    Todays question is how to easily limit a number to "not less than zero".

    I have a number... lets call it "volume" and I want to increment or decrement that number by "step", which could be from 1 to 9.
    I want to limit "volume" to no more than 350 at the upper end and no "less" than zero at the low end.

    So if I press buttonA then volume increments by the step amount, and if I press buttonB then volume decrements by step amount.

    Here's a simple example (of what doesn't work):
    Code:
     
     volume = 100        'set a default level
     step = 3               'This could be ANY NUMBER FROM 1 TO 9
    
     
     main:
    
     if buttonA  then volume = (volume + step) min 350     'this works fine to limit the top end to 350
    
     if buttonB  then volume= (volume - step)  max 0        'but of course THIS fails, because it's hard to go lower than zero
    
     LCDOUT $fe, $c0, dec volume
    
     pause 50
     goto main
    
     end
    So I'm sure that there must be an easy way to limit a number from decrementing "below" zero... ?
    Help please?


    Thanks much!

    Steve

  2. #2
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Code:
    if buttonB  then 
        if step > volume then 
              volume = 0
        else
             volume= (volume - step) 
        endif
     endif
    Charles Linquist

  3. #3
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    This Should (untested as of yet) work...

    One thing about programming... there is always more than one way to do the exact same thing... some are more elegant than others... Mine usually falls into the "less elegant, but gets the job done" category

    The extra "IF" test should eliminate the need to test for below zero or the "max 0" test.

    Code:
    volume = 100        'set a default level
     step = 3            'This could be ANY NUMBER FROM 1 TO 9
    
     
     main:
    
     if buttonA  then volume = (volume + step) min 350     'this works fine to limit the top end to 350
     
     if buttonB and (volume < 3) then  
          volume = 0
          goto Skip1
          endif  
     
     if buttonB  then volume= (volume - step)         'but of course THIS fails, because it's hard to go lower than zero
    
    Skip1:
     LCDOUT $fe, $c0, dec volume
    
     pause 50
     goto main
    
     end
    Last edited by Heckler; - 17th September 2011 at 04:18.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  4. #4
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Hey Charles... you beat me to it

    When I started my reply, you had not posted yet. I am not sure I like the new code posting window... it gave me fits and I had to start over.

    I think I like yours better... but it is fun and informative to try different bits of code and compile each and see how the byte count changes. I have been surprised at times in finding that what you think should be more compact code ends up being more bloated.
    Last edited by Heckler; - 17th September 2011 at 04:26.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Yup, there are always lots of ways.

    Code:
    if buttonB then
        volume = volume - Vstep        ; subtract the step
        if volume.15 then volume = 0   ; zero if it went negative
    endif
    Last edited by Darrel Taylor; - 17th September 2011 at 04:45. Reason: step is a reserved word
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Code:
    if buttonB then
     if volume - Vstep >= vstep then
      volume = volume - Vstep
     else
      volume = 0   
     endif
    endif
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Straying a little off...

    I'm never more than a couple of threads away from having it underlined that I'm still woefully underskilled in this programming melarkey - where's the condition here...
    Code:
    if buttonB then
    to my n00b eyes, I'd have thought there should be a condition like "if buttonB = 0 then" or "if buttonB = 1 then"

    ??

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