limiting to "no less than" zero


Closed Thread
Results 1 to 31 of 31

Hybrid View

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

    ??

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    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

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

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    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.

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Thanks guys!

    Well... I guess the answer to my question is that there isn't really a way that's as easy as using "min" to limit the upper end.

    The method that Charles posted is what I've been using. But I was hoping for something that was... simpler.
    I guess I'd hoped there was some way as clean and easy as "MAX" but that worked all the way down to zero.

    Darrels method using volume.15 is great, but it "wastes" half of a variable.
    Most of the variables that I need to limit are declared as bytes, and I need the full 255 step range for some of them.
    I *can* use it for my temperature setpoint because that needs a word anyway and I can afford to "lose" part of it.

    I'm starting to run low on code space on this project so hoping to find more efficient ways to do a few things...

    Thanks for the input!

    Steve

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

    Quote Originally Posted by Byte_Butcher View Post
    Darrels method using volume.15 is great, but it "wastes" half of a variable.
    Most of the variables that I need to limit are declared as bytes, and I need the full 255 step range for some of them.
    With 8-bit values, the CARRY flag is valid after addition or subtraction.

    So it can be ...
    Code:
    if buttonB then
        Volume = Volume - Vstep
        IF !STATUS.0 THEN Volume = 0   ; Zero if BORROWED
    endif
    DT

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    STATUS ?
    STATUS.0 ?

    Ahhh, Darrel, you confuseth me. From whence comes... STATUS ?

    steve

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