Simple Maths Going Wrong


Results 1 to 40 of 44

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    Here's one that works real well.
    But it requires PBP 2.60 for the ATN function.

    The measured wind direction should be in the variable DIR.
    Then GOSUB Avg_Wind

    It works by averaging the SIN and COS separately for each sample.
    Then converts those averages back to an angle with ATN.
    Angles range from 0-255, representing 0-359 degrees.
    Code:
    CLEAR
    
    DIR   VAR BYTE   ; Current sample of wind direction (0-255)
    S     VAR BYTE   ; averaged SIN
    C     VAR BYTE   ; averaged COS
    Wind  VAR BYTE   ; final averaged wind direction
    Temp  VAR BYTE   ; temporary variable
    
    AvgCount  CON 10 ; amount of averaging to apply
    
    ;---------------------------------------------------------------
    Avg_Wind:    ; average in one DIR sample
      Temp = SIN DIR+127
      S=(S*(AvgCount-1)+Temp)/AvgCount
      Temp = COS DIR+127
      C=(C*(AvgCount-1)+Temp)/AvgCount
      Wind = (C-127) ATN (S-127)
    RETURN
    
    ;---------------------------------------------------------------
    Set_Wind:    ; Sets average wind direction without averaging
      S = SIN DIR + 127
      C = COS DIR + 127
    GOTO Avg_Wind
    If you don't have 2.60, the same thing can probably be done with scalerobotics cordic trig routines.

    HTH,
    Last edited by Darrel Taylor; - 22nd July 2009 at 16:08. Reason: Fixed a math overflow prob
    DT

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 10:45
  2. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 06:19
  3. Simple LCD code not working!...WHY?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th November 2009, 19:48
  4. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 01:11
  5. Doing Simple Math - getting the wrong answer
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2005, 14:27

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