I'm using the values from a potentiometer and mapping it to 0-255 with very jittery results.
ie.
int Constrainedaverage = constrain(potmovingavg,0,1000);
Pot1MappedOutputValue = map(Constrainedaverage, 5, 1000, 0, 255);
when the average value read from pot is certain values, for example 550 the map value fluctuates between 139 & 140
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
pot1RAWValue : 759 / Constrainedaverage : 759 / potmovingavg: 759 / Pot1MappedOutputValue: 193
pot1RAWValue : 758 / Constrainedaverage : 758 / potmovingavg: 758 / Pot1MappedOutputValue: 192
and with
int Constrainedaverage = constrain(potmovingavg,0,999);
Pot1MappedOutputValue = map(Constrainedaverage, 5, 999, 0, 255);
the map value fluctuates wildly as 197 & 196
pot1RAWValue : 550Constrainedaverage : 550 / potmovingavg: 550 / Pot1MappedOutputValue: 139
pot1RAWValue : 551Constrainedaverage : 551 / potmovingavg: 551 / Pot1MappedOutputValue: 140
pot1RAWValue : 550Constrainedaverage : 550 / potmovingavg: 550 / Pot1MappedOutputValue: 139
pot1RAWValue : 551Constrainedaverage : 551 / potmovingavg: 551 / Pot1MappedOutputValue: 140
I tried understanding what the issue might be from here:
but exceeded my capacity. Any takers?
Full code below:
//#include "FastLED.h" // FastLED library.
#include <movingAvg.h>
////////////////////////////////////////////POTENTIOMETER1 / BRIGHTNESS
int pot1 = A5;
int pot1RAWValue = 0;
int pot1OldRAWValue = 0;
const int numReadings1 = 10;
int readings1[numReadings1]; // the readings from the analog input
int index1 = 0; // the index of the current reading
int total1 = 0; // the running total
int average1 = 0; // the average
int Pot1MappedOutputValue = 32;
int oldPot1MappedOutputValue = 0;
movingAvg potentiometeraverage(25);
////////////////////////////////////////////POTENTIOMETER1////////////////////////////////////////////
////////////////////////////////////////////////SETUP/////////////////////////////////////////////////
void setup() {
Serial.begin(57600);
potentiometeraverage.begin();
}////////////////////////////////////////////////SETUP/////////////////////////////////////////////////
////////////////////////////////////////////////LOOP/////////////////////////////////////////////////
void loop() {
/////////////////////////////////////////////// TGAN_Potentiometer_1
pot1RAWValue = analogRead(pot1); // Serial.print("pot1RAWValue : "); Serial.println(pot1RAWValue );
//Discard the value if its less that +2 of the old value. Removes jittery readings.
if (abs(pot1RAWValue-pot1OldRAWValue)<= 3)
{
pot1RAWValue = pot1OldRAWValue;
}
int potmovingavg = potentiometeraverage.reading(pot1RAWValue);
int Constrainedaverage = constrain(potmovingavg,0,1000);
Pot1MappedOutputValue = map(Constrainedaverage, 5, 1000, 0, 255);
if (Pot1MappedOutputValue != oldPot1MappedOutputValue)
{
Serial.print("pot1RAWValue : "); Serial.print(pot1RAWValue ); Serial.print(" / Constrainedaverage : "); Serial.print(Constrainedaverage ); Serial.print(" / potmovingavg: "); Serial.print(potmovingavg); Serial.print(" / Pot1MappedOutputValue: "); Serial.println(Pot1MappedOutputValue);
}
oldPot1MappedOutputValue = Pot1MappedOutputValue;
/////////////////////////////////////// TGAN_Potentiometer_1 ///////////////////////////////////////
}