Craig already explained the reason of the error is the lack of a default
constructor, and chrisl suggested a reasonable alternative. I would like
to suggest two other alternatives.
Static initialization
You state that you want to do the initialization in setup()
, but you
don't explain why you want that. I will guess that what you really
want is to have all those objects in array. The library page provides
this example showing how to statically initialize multiple instances:
ResponsiveAnalogRead analogOne(A1, true);
ResponsiveAnalogRead analogTwo(A2, true);
The problem with this example is that you don't have an array, so you
cannot loop through the instances. This problem, however, can be easily
fixed:
const size_t ANALOG_SIZE = 3;
ResponsiveAnalogRead analog_reads[ANALOG_SIZE] = {
ResponsiveAnalogRead(A0, true),
ResponsiveAnalogRead(A1, true),
ResponsiveAnalogRead(A2, true)
};
Note that you don't need the ANALOG_PINS
array anymore. And you don't
need to pass 0.01
as the last argument to the constructor, as this is
the default value anyway. This would be my preferred solution: it is
simple enough and doesn't increase the number of lines of code.
Patching the library
If you really want to initialize the array in setup()
, then you can
patch the file ResponsiveAnalogRead.h from the library:
- Add default values to all the parameters of the constructor. For
example:
ResponsiveAnalogRead(int pin=0, bool sleepEnable=0, float snapMultiplier=0.01);
- Add a method for setting the pin after initialization:
void setPin(int newPin) { pin = newPin; pinMode(pin, INPUT); }
The first change will enable you to compile your code as-is. The second
change will allow you to update the objects in setup()
rather than
overwriting them (which I don't find very elegant):
for (size_t i = 0; i < ANALOG_SIZE; i++) {
analog_reads[i].setPin(ANALOG_PINS[i]);
analog_reads[i].enableSleep();
}
Note that using the default constructor now has the side effect of
setting pin 0 to input. This should not be a big deal, as the
constructor is called before setup()
, and all the IO pins are inputs
at this point. If you want nevertheless to void that side effect, then
set the default pin to -1
, and patch the constructor to only configure
the pin if it's not -1
.
Patching the library creates a maintenance burden though: you will have
to reapply the patch each time the library is updated. This may not be
that bad if you know how to git rebase
or git merge
, but ideally you
would want to get a pull request accepted by the original maintainer.
Edit: Before patching the library, consider weighting in this
issue and using this fork.
Update: A pull request addressing this issue has been
accepted. As of version 1.2.0, the class has now a default “do nothing”
constructor.