Shop OBEX P1 Docs P2 Docs Learn Events
confused on averages? — Parallax Forums

confused on averages?

moomoomoomoo Posts: 27
edited 2005-04-25 13:38 in BASIC Stamp
I've got the Memsic 2125 input streaming into the microcontroller. I need to take five readings, taking out the high and low number and averaging the other three to get the number to use for my output. I'm storing the five variables in
Temp var word(5), I just don't know how to deal with it after this?
I could If...Then my program to death, but surely there is a better way.

My brain is collapsing, so anybody that can help me would be most appreciated!

By the way, this is for a balance routine if that helps.

·

Comments

  • edited 2005-04-25 03:19
    If you are taking an average, you can just keep a running sum. If you want to discard the highest and lowest, use IF...THEN to store the largest and smallest values in separate variables. Subtract the largest and smallest variables from the sum, then divide by 3.

    Something like this (you will have to complete it).

    GOSUB Acceleromter

    xsum = x
    xMax = x
    xMin = x

    for counter = 1 to 4

    · GOSUB Accelerometer
    · xSum = xSum + x
    · IF x > xMax THEN xMax = x
    · ' More code not shown
  • moomoomoomoo Posts: 27
    edited 2005-04-25 13:38
    So, then something like this to finish off...

    If x > xMax then xMax = x
    xSum = ABS(xSum - xMax) 'ABS to keep out the negatives
    Esleif x < xMin
    xSum = ABS(xSum - xMin)
    Else
    xSum = xSum + x

    xTotal = xSum / 3

    I was thinking, wouldn't xSum + x be later in the IF...Then statement instead of before, so I could elimate
    the Max and Min before adding to Sum. Correct if I'm thinking wrong.
Sign In or Register to comment.