How to detect variable going below zero, without using PBPL ?


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    It depends on what you're doing with the number(s).
    If your displaying them using LCDOUT/SEROUT/HSEROUT/ARRAYWRITE you can use the SDEC modifier to display the value 65535 as -1.

    If you're doing further math with the numbers you need check the highest bit, if it's set the value is negative and you can use the ABS operator to retrieve the actual value, do whatever needs done (like a division) and then restore the sign.
    Code:
    X VAR WORD
    Sign VAR BIT
    
    X = -5000
    HSEROUT["Raw: ", DEC X, "    Signed: ", SDEC X, 13]
    
    Sign = X.15    ' Preserve sign
    X = ABS(X) / 5   ' Divide absolute value
    X.15 = Sign   ' Restore sign
    
    HSEROUT[SDEC X, 13]
    If all you want to do is cap the value at 0 then
    Code:
    IF X.15 THEN X = 0

  2. #2
    Join Date
    Feb 2013
    Posts
    1,154


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    I'm helping some fellow students to build an extinction meter.
    There's ADC reading, and based on liquid passed in the system, light transmission, compared to reference, might increase or decrease.
    So idea is, that they flow thru the "reference" liquid, ADC is read and that value is used as offset, which is deducted form further ADC readings, to get the info, what's going on.

  3. #3
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    Code:
    ADCIN Baseline
    ADCIN Reference
    IF Reference < Baseline THEN
      'Sign = negative
      'Use subtraction in calculation
      'Denote a reduction
    ELSE
      'Sign = positive
      'Use addition in calculation
      'Denote an increase
    ENDIF
    Another possibility is use 128 = 0. If ADC Value < 128, treat as negative... I've created my own positive/negative methods that don't follow the standardized Signed Variable format. The math was easier my way for what I was doing. Just some thoughts that might spur ideas.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,154


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    It's not ADC value reference that causes problems.
    Here's the explanation.

    Say ADC changes from 100 to 200 and "middle" point is being 150.
    So to have zero at my system variable output (which I later display both on screen and write to EEPROM), I'm deducting 150 from the input ADC value, to get zero. In case when ADC reading increases, than all is good, final variable value changes from 0 to 50, that's good. But if ADC value gets below 150, then things get bad.

  5. #5
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Thumbs up Re: How to detect variable going below zero, without using PBPL ?

    Code:
    Zero VAR BYTE
    Variable VAR BYTE
    OtherVar VAR BYTE
    
    ADCIN, Zero
    ADCIN, Variable
    IF Variable < Zero THEN
      Zero = [some newly calculated value]
    ENDIF
    OtherVar = Variable - Zero
    Make up your own rules!

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,690


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    But if ADC value gets below 150, then things get bad.
    what does get bad mean ?

    show your code

  7. #7
    Join Date
    Feb 2013
    Posts
    1,154


    Did you find this post helpful? Yes | No

    Default Re: How to detect variable going below zero, without using PBPL ?

    Variable value should go to negative, but since we have no negative, it goes into 65xxx range

Similar Threads

  1. How to detect variable decrease moment?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 4th February 2014, 23:02
  2. PBPL and TCP/IP
    By edtp in forum Ethernet
    Replies: 10
    Last Post: - 4th March 2011, 20:35
  3. Help, PBPL and DIV 32.
    By Rogerio in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 16th March 2010, 11:37
  4. PBP and PBPL
    By keymuu in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 30th January 2009, 18:58
  5. IF..AND/OR..THEN and PBPL
    By duncan303 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th January 2008, 17:45

Members who have read this thread : 1

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