Ok, what exactly should I post? The code is really simple as I said I just use analogRead and divide the value by 8 and when the value I read is different than the previous I send it.Furthermore I have not make any schematic as it is so simple, I am just using one potentiometer. Thank you for your fast response.
P.S. I can suspect only one think. The delay between every reading. I use delay(5).
Post your code, no matter how simple. delay(5) already sounds like you are making problems for yourself.
Dividing by 8 is a valid method of reducing the data but you've already seen that it can bounce up and down by a few points just due to external noise. What if those few points cross an 8-point boundary? Then it will flick up and down by a big step even though the noise is small.
A moving average is the simplest and most effective way of reducing the noise. If you save the last 3 values of your divide-by-8 result, you might get 7, 7, 8. What is the correct output? Obviously 7. (Not 7.3 - we round off to an integer after taking the average.) Since the Arduino works really fast, it is equally feasible to store the last 30 or 300 values.
Here's a very simple class that I use in a lot of places. I never published it as a library because it's actually part of a larger library I wrote myself...
class Average {
public:
void setStrength(int numSamples) {
//should be called rarely, like once in setup and maybe once if the configuration changes
//call begin() after this to re-populate the array and reset the current sample
//numSamples should be a number like 10 or 100
if (numSamples <= 0) return;
_value = (int*)realloc(_value, numSamples * sizeof(int));
_numSamples = numSamples;
_numSamples_2 = numSamples / 2;
}
void begin(int firstValue) {
if(_value==NULL) setStrength(1); //If you forgot to set the strength, default strength = no averaging
_currentTotal = 0;
for (int i = 0; i < _numSamples; i++) {
_value[i] = firstValue;
_currentTotal += firstValue;
}
_currentSample = 0;
}
void update(int sample) {
if (_numSamples < 1) return;
if (_currentSample >= _numSamples) _currentSample = 0;
_currentTotal += sample - _value[_currentSample];
_value[_currentSample] = sample;
_currentSample++;
return;
}
int value() {
return (_currentTotal + _numSamples_2) / _numSamples;
}
Average& operator=(int sample) {
//allows us to use the assignment filterInstance=x; to update the filter
update(sample);
return *this;
}
operator int() {
//allows us to return a default value, for the construction x=filterInstance;
return value();
}
private:
int _numSamples = 0;
int _numSamples_2 = 0;
int* _value = NULL;
int _currentSample = 0;
int _currentTotal = 0;
int _rawValue = 0;
};
You use it like this:
Average myAvg;
const int inputPin = A0;
void setup() {
myAvg.setStrength(30); //use a larger number for "stronger" averaging. Watch out you don't use all your memory if you are on a smaller Arduino like an UNO
myAvg.begin(analogRead(inputPin));
Serial.begin(9600);
while(!Serial && millis()<5000){
//on native USB Arduinos, wait for a USB serial connection
}
Serial.println("Beginning averager");
}
void loop() {
const unsigned long inputDataPeriod = 5; //milliseconds; only take a sample from the input this often
static unsigned long lastDataInput = millis();
const unsigned long outputDataPeriod = 50; //milliseconds; only print to the output this often
static unsigned long lastDataOutput = millis();
static int inputValue;
//read the input on the defined frequency and store in the averager
if(millis() - lastDataInput > inputDataPeriod) {
lastDataInput += inputDataPeriod;
inputValue = analogRead(inputPin);
myAvg.update(inputValue); //may also be written as myAvg = inputValue;
}
//output the average and the last instantaneous reading on the defined frequency
if(millis() - lastDataOutput > outputDataPeriod) {
lastDataOutput += outputDataPeriod;
Serial.print(inputValue);
Serial.print(',');
Serial.print(myAvg);
Serial.println();
}
}
Run this with the Serial Plotter to observe the output. It doesn't attempt to print every input value but by printing the most-recent, you will get some idea of the randomness of the input.