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

    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.

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

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

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

    ??

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,144


    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

  6. #6
    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 12:37.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,144


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Related to the "default" just use the ! before your variable:

    If !buttonB then.... meaning If buttonB=0 then...

    The 15th bit is the sign bit in two's complement. When the most significant bit is 1, it indicates a negative sign. Just Binary maths...

    But is relative. If you use 8-bits then the 8th (most significant bit) is considered the sign bit. As long as you are thinking of positive and negative numbers. So at the end you have from -128 to +127 (1000 0000 to 0111 1111).

    http://en.wikipedia.org/wiki/Two's_complement

    Ioannis
    Last edited by Ioannis; - 17th September 2011 at 13:06.

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