So I'm working with pots and I want to smooth out the measurements. So I created a function that takes ten measurements and returns the average and I want to be able to call it the same way I would call analogRead()
. So I created a template for this purpose, here's the code:
template<const uint8_t& PIN>
unsigned smoothReading(const uint8_t& PIN)
{
static unsigned sample[10];
static unsigned currentUnit = 0;
if(currentUnit < 9) currentUnit++;
else currentUnit = 0;
sample[currentUnit] = analogRead(PIN);
double average = 0;
for (const auto& unit : sample)
average += unit;
average = average / 10;
return round(average);
}
As you can see, I have some static variables which can't be shared among function calls for each pin. To be more precise, each pin has its own sample array of measurements, etc. So I have to either write this as a template; or have one single function and global variables for each pin I want to measure; or declare multiple versions of the function for each pin I want to measure. So I prefer just using a template.
The problem is, I keep getting shadows template parm
errors. So,does the compiler think I'm trying to declare two different parameters with the same name? How can I make the template parameter resolve to the function parameter? Is there any other reason for this error?
Aucun commentaire:
Enregistrer un commentaire