Hey Guys,
So this is what I've got.
I've implemented the first of AJ's suggestions.
#include "WeightedValue.h"
#include "WProgram.h"
WeightedValue::WeightedValue(int inputMinTarget, int inputMaxTarget)
{
minTarget = inputMinTarget;
maxTarget = inputMaxTarget;
sensorTop = 0;
sensorBottom = 1024;
readingsSoFar = 0;
}
int WeightedValue::returnWeightedValue(int inputValue)
{
readingsSoFar++;
if (inputValue < sensorBottom)
{
int range = inputValue - sensorBottom;
int change = range / (readingsSoFar);
sensorBottom -= change;
}
if (inputValue > sensorTop)
{
int range = inputValue - sensorTop;
int change = range / (readingsSoFar);
sensorTop += change;
}
int outputValue;
outputValue = map(inputValue, sensorBottom, sensorTop, minTarget, maxTarget);
outputValue = constrain(outputValue, 0, 254);
return outputValue;
}
First question, am I calculating the range correctly?
Last time I tested on the hardware I used if (inputValue < sensorBottom) {int range = inputValue - sensorBottom;}
So this might have messed up the results a little.
Also, I run this code around 25 times per second.
(So after one second, X is already around 25)
So, I should probably tone that impact down a bit?