limiting to "no less than" zero


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    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 05:18.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  2. #2
    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 05:26.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  3. #3
    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 05:45. Reason: step is a reserved word
    DT

  4. #4
    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

  5. #5
    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"

    ??

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,172


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    If buttonB means If buttonB=1.

    Is a default.

    A pure boolean logic.

    Darrel gets the most elegant so far coding

    Ioannis

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    I'd figured as much (on account everyone was doing it!)....can the default be changed to 0? (I'd imagine that switches get used with weak pullups or external pullups ....so the switch pressed condition would be a 0).

    Re Darrel's entry..

    Code:
    if volume.15 then volume = 0 ; zero if it went negative
    how does bit 15 of 'volume' being a '1' indicate negative? Can someone shine a little more light on this one please?
    Last edited by HankMcSpank; - 17th September 2011 at 13:37.

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