Hello
I am back with a mighty question
.... or maybe not...
I am re-writing a class to read potentiometers. It works well, but I want to take it a step further by removing all the Serial.print bits in the loop section of the sketch.
Basically I would like to achieve the same result with just one line in the main code. I am not sure if this can be achieved. To do this I was hoping to create a static function in the class so that when I call this function, it prints out the values of a specific variable (or return value of a function) for each instance of the class (for all of instances to be clear). In this case the return value of the function readValue() for each object.
I am struggling with the fact that I am attempting to get the value without naming the object when I do that so I get the expected error ...
I will show it below:
#define DEBUG
#include "Potentiometer.h"
#define POT_NUM 6 // number of potentiometers
Potentiometer Pot[POT_NUM] = {(A0), (A1), (A2), (A3), (A4), (A5)}; // creates a number (POT_NUM) of objects of Potentiometer class and passes the pin number as argument
int potReading [POT_NUM]; // variable to store the returned potentiometers reading values
//**********************************************************************************************************************************************
void setup() {
#ifdef DEBUG
Serial.begin(9600);
Serial.print("Number of pots: ");
Serial.println(Potentiometer::getCounter());
#endif
}
//**********************************************************************************************************************************************
void loop() {
for(int i = 0 ; i < POT_NUM; i ++){ // read the pot values
potReading [i] = Pot[i].readValue();
}
#ifdef DEBUG
for(int i = 0 ; i < POT_NUM; i ++){ // this is the part I want to replace
Serial.print("Pot n");
Serial.print(i + 1);
Serial.print("\t");
Serial.print(potReading [i]);
Serial.print("\t");
if (i == POT_NUM - 1){Serial.println();}
}
#endif
}
this is the header file:
#ifndef POTENTIOMETER_H
#define POTENTIOMETER_H
#include <Arduino.h>
class Potentiometer {
private:
byte _pin;
static const int _threshold = 5; // pot threshold to reduce jittering, default 5
static const float _alpha = 0.75; // smothing filter coefficient, default 0.75
static const int _sampleRate = 20;
static int _counter;
int _lastValue = 0; // last potentiometer value
float _leakyAverage; // leaky average variable
int _rawOutput; // output pre-smoothing filter
int _smoothOutput; // output post-smoothing filter
unsigned long _prevMillis;
public:
Potentiometer(){}
Potentiometer(byte pin){
_pin = pin;
_counter++;
}
int readValue(); // method to read potentiometer value
static int getCounter();
// static void printPotValues();
};
#endif
and this is the CPP file:
#include "Potentiometer.h"
int Potentiometer::readValue(){
int val = analogRead(_pin);
if (_threshold <= abs(val - _lastValue)) {
_lastValue = val;
}
float lastValueFloat = _lastValue;
unsigned long now = millis();
if (now - _prevMillis >= _sampleRate){
_prevMillis += _sampleRate;
_leakyAverage = _alpha * _leakyAverage + (1 - _alpha) * lastValueFloat;
}
_rawOutput = (int)lastValueFloat >> 3;
_smoothOutput = (int)_leakyAverage >> 3;
return _smoothOutput;
}
int Potentiometer::_counter = 0;
int Potentiometer::getCounter(){
return _counter;
}
// void Potentiometer::printPotValues() {
// }