LED 'Dot' Bargraph Issues - Newbie Code!


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Wow, I cannot thank you enough Darrel! What elegant and efficient code! The difference between a newbie and a master. :-)
    I will study the code and get familiar with LOOKUP (never used that fuction before) as well as understanding how it all works.

    For example, setting Row = -1, I never knew you could set to a minus value. Don't really know what that is.

    Brilliant code. Thanks a ton!!

  2. #2
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Hi Darrel,
    I tried the averaging routine but could not get it to work. In the code is has:

    Code:
    GOTO OverAverage
    ' -=-=-=-=-=-=  Average Analog values -=-=-=-=-=-=-=-=-=-=
    however, there is no OverAverage routine. Also, I replaced the word "Value" with "adval", but that did nothing.

    Sorry, I am just not seeing this.
    Thanks!

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    The OverAverage label is at the end of the code in that thread.
    Maybe you didn't copy enough of it.
    DT

  4. #4
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Hi Darrel,
    Here is the entire code on that link:
    Code:
    '****************************************************************
    '*  Name    : Average_Hyst.pbp                                  *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 7/19/2005                                         *
    '*  Version : 3.0                                               *
    '*  Notes   : Modified to use Hysteresis                        *
    '*          : Use as an INCLUDE file                            *
    '****************************************************************
    ' This routine will keep a "Running Average" of 10-bit Analog Values 
    ' Instead of adding a bunch of values together and then dividing by the number 
    ' of samples, it averages each sample into the final result immediately.
    ' This eliminates the need for 32 bit math.
    ' To allow it to "Catch up" to large changes, set the FAspread to an 
    ' acceptable range.
    ' Simply place the new number in VALUE and GoSub Average 
    ' The Average (with Hysterisis) will be returned into the same variable VALUE
    
    '---- [ User Options ] ---------------------------------------------------------
    HystLevel   CON  7      ' 7 = .7 of the Least Significant Digit
                            '    Valid HystLevel is from 6 to 13
    AvgCount    CON  16     ' = Number of samples to average. For best response 
                            '    times, keep AvgCount as small as you can
    FAspread    CON  50     ' = Fast Average threshold +/-
                            '  FAspread should be larger than the worst possible
                            '  noise in the A/D conversion.
    '-------------------------------------------------------------------------------
    
    AVGchanged  VAR  BIT    ' 1 indicates that the Average Value has changed
                            ' you have to clear this bit after using it
    ADavg       VAR  WORD   ' Stores the current Average
    ADhyst      VAR  WORD   ' Stores the current Value for Hysterisis
    Value       VAR  WORD   ' Input / Output variable to be averaged
    spread      CON  FAspread * 10  ' adjust spread *10 for Hyst.
    
    GOTO OverAverage
    ' -=-=-=-=-=-=  Average Analog values -=-=-=-=-=-=-=-=-=-=
    Average:
        Value = Value * 10                      ' Scale value up for hysterisis
        IF Value = ADavg Then NoChange          ' if they're the same, nothing to do 
        IF ABS (Value - ADavg) > spread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)         ' Subtract 1 samples worth
        ADavg = ADavg + (Value/AvgCount)         ' Add in the new sample portion 
        GoTo AVGok
      FastAvg:                                   ' Catch up to the changing value
        ADavg = Value                            ' Set Average to the current value
        GoTo AVGok
      RealClose:                                 ' Reduce the sample size when
        ADavg = ADavg - (ADavg/(AvgCount/4))     ' the average and the sample are
        ADavg = ADavg + (Value/(AvgCount/4))     ' "Real Close"
      AVGok:
        IF ABS (ADavg - ADhyst) > HystLevel then ' If it changed > HystLevel +/-
            ADhyst = ((ADavg + 5) / 10) * 10     ' Round ADavg to get new Value
            AVGchanged = 1                       ' Indicate that Average changed
        ENDIF   
      NoChange:
        Value = ADhyst / 10                      ' Scale the result down
    Return
    
    OverAverage:
    '---------------------------------------------------------------
    It may not have been put in there properly. Can you click the link and see what you get?

    From the code, it jumps over the section staring with:
    ' -=-=-=-=-=-= Average Analog values -=-=-=-=-=-=-=-=-=-=

    It does not seem to make sense to me.

    Thanks. :-)
    Last edited by SOTASOTA; - 14th February 2013 at 05:09.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Hi,
    The label OverAverage is right at the bottom of the code you just posted.
    Usually you include the file somewhere around the top of your program. When the program starts from the beginning it eventually reaches the code which is in the include file. If you don't want it to execute the code at that point you must jump over it and that's exactly what's happening here.

    Then, when you actually WANT to execute the code from within your main routine or whatever you GOSUB Average, that way it will enter the code after the GOTO OverAverage and execute the code until it hits the RETURN which returns the execution to the place it was called from.

    /Henrik.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    The averaging code is an INCLUDE file.

    Save it to a file called "Average_Hyst.pbp".
    Put the file in the same folder as your project, or the PBP folder.

    Then at the top of the code in post #4, add this line.
    Code:
    INCLUDE "Average_Hyst.pbp"
    The GOTO OverAverage line just jumps over the code, making sure execution doesn't "Fall Into" the subroutine without being called by a GOSUB.

    Then, change these lines to use the "Value" variable, instead of adval.
    Code:
    Main: 
        ADCIN 0, Value
        GOSUB Average
    
        LED = Value * LEDcount                  ; Determine which LED to light
        LED = DIV32 (1023 - Margin) - 1
    Edit: Whoops, sorry Henrik.
    Took me an hour to get his darn post to go through.
    The forums going up and down like an elevator.
    Ya beat me to it.
    Last edited by Darrel Taylor; - 14th February 2013 at 06:45.
    DT

  7. #7
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    OK, that is what I thought but could not get my head around it. Too many late nights!
    Thanks, I will give it a shot and see if it works.
    I sure learn a great deal on these forums. I have to start using the LOOKUP command more...very useful!!

  8. #8
    Join Date
    Jan 2009
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: LED 'Dot' Bargraph Issues - Newbie Code!

    Alrighty, I just implemented the code and for the most part it works real well! Thanks!
    I still get flickering on a single LED now at some in-between value.

    Not sure it this can be gotten rid of and have a nice solid LED. I have these values in:

    '---- [ User Options ] ---------------------------------------------------------
    HystLevel CON 13 ' 7 = .7 of the Least Significant Digit
    ' Valid HystLevel is from 6 to 13
    AvgCount CON 32 ' = Number of samples to average. For best response
    ' times, keep AvgCount as small as you can
    FAspread CON 100 ' = Fast Average threshold +/-
    ' FAspread should be larger than the worst possible
    ' noise in the A/D conversion.
    '-------------------------------------------------------------------------------

Similar Threads

  1. Replies: 3
    Last Post: - 24th March 2015, 11:51
  2. Replies: 19
    Last Post: - 26th April 2012, 20:23
  3. Newbie needs example RC code
    By PickyBiker in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 18th March 2010, 02:10
  4. Microchip Code Issues
    By Destovi in forum General
    Replies: 4
    Last Post: - 20th March 2008, 13:32
  5. BaseBall pitching Code sample - NewBie
    By foxstar in forum Code Examples
    Replies: 12
    Last Post: - 8th May 2007, 19:29

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