Sonic Depth Meter code & Math problems 12F683


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    Have I defined my variables correctly?

  2. #2
    Join Date
    Jan 2009
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    Sorry, and yes, runs once to establish one depth value based on the average of 10 readings. I have an external system that cycles the power every 5 min ultimately giving me an average reading every 5 min.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,121


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    I 'd use what Henrik has posted and maybe with a smaller multiplier, say 3000. Keep your ten average pile. Then Henrik's lines will give you directly the result for your digital pot.

    Code:
    'Main Program  ****************************
    
    'get avg depth
        pause 1000
        for x = 1 to 10
            pulsin gpio.4,1,inputpulse
            dist= dist + inputpulse
            pause 100
        next x
        
        newtapsetting = (dist ** 3060) - 42       'try also a number smaller, 3000
        IF newtapsetting > 127 THEN newtapsetting = 127
    
    serout gpio.0,t9600,[12,"avg :", #avgdist,13]
    serout gpio.0,t9600,["tap :", #newtapsetting]
    
    pause 500
    
    end
    Ioannis
    Last edited by Ioannis; - 29th October 2013 at 18:04.

  4. #4
    Join Date
    Jan 2009
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    Thanks guys. I'm trying to understand the math here, help me out.

    What is the difference between * and **? I can't seem to find something on this forum that explains this. How does the math work.

    For example, Henrik's suggestion above, how did you select 3060 to try and result in a range between 1 and 127? I tried random numbers and 2020 gets me close to my 127 value at full, but gives me 25 at empty. If I subtract 24 to give me 1, then I've also reduced my 127 to 103...Ugg.

    and while I'm at it...how doe the */ and // work?


    I really need the result to provide a linear value between 1 and 127 for this to work properly. So I feel like I need to determine the ratio of the reading (in this case with full reading 92 and emplty reading 364, I would subtract 91 from the values for an adjusted range between 1 and 273) then take the adjusted value and divide by 273 for a multiplier between 0 and 1 and apply that multiplier to 127.
    Last edited by johnnylynx; - 30th October 2013 at 05:01.

  5. #5
    Join Date
    Jan 2009
    Posts
    34


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    Excuse my last reply...Henrik's number of 3020 gets me a new tap setting of 127 (perfect empty value) but when the tank is full (8" away from the sensor) the new tap reading is 42 (not 0 or 1). If I subtract 42, the full reading gets to 0 or 1, but my empty reading is also reduced...not letting me see the empty value of 127.


  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,610


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    Hi,
    I guess I'm a bit confused by your somewhat "conflicting" numbers.
    In your original post you said that the input value ranged from 92 from to 466 but that a full scale output (127) should be reached with an input value of 364.
    So, I need to convert the 92 and 364 to 1% or 100% respectively and apply to 127 to get a new tap setting
    Think of the ** operator as kind of like multiplying by units of 1/65536. With an input value of 920 (given we stick with the times ten accumulated result) the output value would (should) be 920 * (3060/65536) = 42 and with an input value of 3640 the output value will be 3640 * (3060/65536) = 169. Subtract 42 from the two numbers and you'll get 0 and 127 respectively. Note that it's 3060, not 3020.

    Is that not what you wanted? Or is that what you wanted but it doesn't actually give you those results - which is possible as I haven't tested it here. Please clarify.

    The */ operator is similar to the ** operator but instead of units of 1/65536 it's units of 1/256. The // operator is modolus or remainder. If you do 123//10 you'll get 3 which is the "left over" from 123/10. They have been covered multiple times on the forum and they are covered in the manual.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Sonic Depth Meter code & Math problems 12F683

    And if I may add, to prevent negative results, a limit test for the two sides of the range will guarantee the result will be 0-127.

    Ioannis

Similar Threads

  1. Replies: 0
    Last Post: - 27th March 2011, 20:10
  2. PIC 12f683 AD/PWM Problems:
    By surfer0815 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th December 2005, 16:48
  3. math problems (again)
    By lab310 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th November 2005, 22:43
  4. 12F683 Comparator Problems
    By morethanenuf in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 9th September 2005, 15:36
  5. math problems - large numbers
    By Tomasm in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th February 2004, 07:48

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