Iterate through multiple objects

Hi Guys,
I’m working on a digital instrument and I use the awesome ResponsiveAnalogRead library to smoothen my sensor values before using them. I want to read and smoothen a total of 6 analog inputs but currently I’m initializing and updating them “by hand”. Is there a more elegant way of initiating/updating/reading the analog read objects by iterating through them? I thought that I might be able to put them in an array but since they are objects/classes, I don’t really know how approach it.

// Code for example
#include <ResponsiveAnalogRead.h>

ResponsiveAnalogRead pos1(A0, true);
ResponsiveAnalogRead pos2(A1, true);
ResponsiveAnalogRead pos3(A2, true);
ResponsiveAnalogRead pos4(A3, true);
ResponsiveAnalogRead pos5(A4, true);
ResponsiveAnalogRead pos6(A5, true);

pos1.setsnapMultiplier(0.01);
pos2.setsnapMultiplier(0.01);
pos3.setsnapMultiplier(0.01);
pos4.setsnapMultiplier(0.01);
pos5.setsnapMultiplier(0.01);
pos6.setsnapMultiplier(0.01);

pos1.setAnalogResolution(4096);
pos2.setAnalogResolution(4096);
pos3.setAnalogResolution(4096);
pos4.setAnalogResolution(4096);
pos5.setAnalogResolution(4096);
pos6.setAnalogResolution(4096);

void setup() {
 Serial.begin(9600);
}

void loop() {
pos1.update();
pos2.update();
pos3.update();
pos4.update();
pos5.update();
pos6.update();
}

void sendNoteOn(int j){

pos1.getValue();
pos2.getValue();
pos3.getValue();
pos4.getValue();
pos5.getValue();
pos6.getValue();
}
// Code for example
#include <ResponsiveAnalogRead.h>

ResponsiveAnalogRead pos[]
{
    {A0, true},
    {A1, true},
    {A2, true},
    {A3, true},
    {A4, true},
    {A5, true}
};

int countRAR = sizeof(pos) / size(pos[0]);

for (int n = 0; n < countRAR; n++)
{
    pos[n].setsnapMultiplier(0.01);
    pos[n].setAnalogResolution(4096);   
}
1 Like