How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,153


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    And since here is PBP forum, not general representation forum, I'm asking how to deal with this in PBP

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Will this work for you?

    iflag var bit
    let iflag =0

    reader:
    adcin 0,Z
    pause 100
    IF Z>100 AND Z<130 THEN I=I+5
    IF Z<100 and iflag = 0 THEN I=I-5 'only if iflag = 0 it's OK to substract
    if i>255 then i=255
    if i<5 then i=0
    if i = 0 then let iflag = 1 'if i hits 0 put up a flag
    if i >= 5 then let iflag = 0 'once i hits 5 or more, reset the flag
    HPWM 2,I,10000
    XY=I
    gosub decoder
    goto reader

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Try this:

    if Z.15 = 1 then
    Z = 0
    endif
    Last edited by Dick Ivers; - 22nd June 2020 at 21:09.

  4. #4
    Join Date
    Aug 2011
    Posts
    457


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    In your original code, change
    Code:
    IF Z<100 THEN I=I-5
    to this
    Code:
    IF Z<100 AND I>=5 THEN I=I-5

Similar Threads

  1. Assigning a string variable to another string variable
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd June 2013, 18:55
  2. Variable within Variable
    By munromh in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th January 2009, 15:44
  3. Electrical Current Does Prevent Corrosion?
    By T.Jackson in forum Off Topic
    Replies: 13
    Last Post: - 18th March 2008, 14:13
  4. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 01:18
  5. 2 variable
    By isaac in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th May 2004, 22:59

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