This is my first attempt at making a library. It is basically a helper class to help with code reuse across the same board. Variables that I have declared in the constructor seem to not set off any compiler flags, however variables that I want to set dynamically in this function
void Esper::configAcs(unsigned int sensitvity, int zeroPoint, unsigned long sampleTime, unsigned long numSamples) {
_acsSens = sensitvity / 1000;
_acsZeroPoint = zeroPoint;
_acsSampleTime = sampleTime;
_acsNumSamples = numSamples;
// the sampling interval, must be longer than then ADC conversion time
_acsSampleInterval = _acsSampleTime / _acsNumSamples;
}
is causing and issue with the compiler in this function:
float getACCurrent(void) {
long currentAcc = 0;
int count = 0;
unsigned long prevMicros = micros() - _acsSampleInterval;
while (count < _acsNumSamples) {
if (micros() - prevMicros >= _acsSampleInterval) {
long int adcRaw = readMcpADC() - _acsZeroPoint;
currentAcc += (long)(adcRaw * adcRaw);
++count;
prevMicros += _acsSampleInterval;
}
}
float rms = sqrt((float)currentAcc / (float)_acsNumSamples) * _vref / (4096 * _acsSens);
return rms;
}
with all of the _acs variables being "not declared in this scope" despite being declared in the .h file.
Hopefully it will make some sense what I am trying to accomplish and someone can help me out here.
WIP class files attached.
Esper.cpp (2.59 KB)
Esper.h (894 Bytes)
Why are the the _acs variables declared as protected ?
UKHeliBob:
Why are the the _acs variables declared as protected ?
They were private before, then I changed them to protected to see if that was it (it wasn't).
I don't want them to be directly editable (go through setter function).
You defined it like normal external function which has no access to the members of your class.
Any class method have to have defined body like:
float Esper::getACCurrent(void) {
...
}
...and you probably want to do this - init of variables:
unsigned long _acsSampleTime = 100000UL;
unsigned long _acsNumSamples = 1000UL;
unsigned long _acsSampleInterval = 0;
byte _mcpAddr = 0x4D;
inside of constructor.
Budvar10:
You defined it like normal external function which has no access to the members of your class.
Any class method have to have defined body like:
float Esper::getACCurrent(void) {
...
}
Sweet baby jesus. This is embarrassing. Thank you 
Can you explain why I should do default assignments in the constructor? In php you can do either or and doesn't really matter.
...I edited my post while you've sent response... read again.
Budvar10:
...I edited my post while you've sent response... read again.
I edited my response. Can you take a look if you haven't already?
we missed each other in time.
Each instance of the class gets its own copy of class variables. Each of this copy have to be init by self constructor and can be changed by self methods so during the time each copy of specific variable can have different value.
Another option is to make the variable shared by all instances of the class. Those can be initialized as you tried.
Budvar10:
we missed each other in time.
Each instance of the class gets its own copy of class variables. Each of this copy have to be init by self constructor and can be changed by self methods so during the time each copy of specific variable can have different value.
Another option is to make the variable shared by all instances of the class. Those can be initialized as you tried.
So are you saying if I have instances:
Esper instance1(somevara);
Esper instance2(somevara);
and I do:
instance1.configAcs(somevarb)
which for simplicity let's say only changes one variable (overriding _acsSensitivity to .6 from .4)
then instance 2 will also have this variable as .6? or no (.4)? because that would be weird.
I basically just want what is defined as you mentioned to be the same across all instances unless an instance changes them through a setter, in which case the change will only affect that particular instance.
Given that I only plan to have one instance this isn't really an issue, but I still wish to learn for future reference
Take a look at class variables and instance variables. Class variables are marked static and are shared among every instance of your class.
Instance variables belong to a single object. So if you declare plain old instance variables for your example, instance1 and instance2 will have their own copy of _acsSensitivity. if you made that variable static, they would share a single copy.
For your purposes, leave out the static modifier and initialize _acsSensitivity to .4 where you declare it. Or if you prefer, set it in the constructor.