Potentially dumb question alert - incrementing variable names

so, in my code I've got 3 pins, lets call them WidgetPin1, WidgetPin2, WidgetPin3. These are then read into the adc to produce WidgetRead1, WidgetRead2, WidgetRead3 (you might be able to spot a trend here).

When the code is running, a number of operations are performed, first widgetpins 1-3 are analogRead into the adc by 3 separate statements, then the resulting widgetread values have a few maths operations performed on them.

At the moment the code looks similar to (its all pseudo code, since the code I've written is confusing/messy/amateur);

<routine to get adc values>
widgetread1=analogread(widgetpin1)
widgetread2=analogread(widgetpin2)
widgetread3=analogread(widgetpin3)

<routine to do funky maths and clever stuff>

widgetread1=float(widgetread1*cleverstuff)
widgetread2=float(widgetread2*cleverstuff)
widgetread3=float(widgetread2*cleverstuff)

<routine to print results to lcd>

setcursor 0,0
print preface text
print widgetread1

setcursor 0, 30
print preface text
print widgetread2

setcursor 0, 60
print preface text
print widgetread2

So, it all looks very mechnical and Im wondering if theres a way to clean it up using for loops with the variable names automatically incrementing. The cursor position bit seems easy enough, its stumping me on autoincrementing the variable names.

Anyway, to me it seems logical (you might spot early onset insanity in this statement) and something others are bound to have come across. Over to you guys (and gals of course).

Arrays!

const int NUM_PINS = 3;
int widgetpin[NUM_PINS] = {1, 2, 3};
int widgetreadings[NUM_PINS];
...

for (int i=0; i<NUM_PINS; i++)
{
  widgetreadings[i] = analogRead(widgetpin[i]);
}

Thanks for the speedy reply! I must admit i was under the impression that arrays were memory inefficient?

scrumfled:
Thanks for the speedy reply! I must admit i was under the impression that arrays were memory inefficient?

What gave you that impression?

scrumfled:
Thanks for the speedy reply! I must admit i was under the impression that arrays were memory inefficient?

You are probably thinking of Strings (capital S).

Arrays are an excellent mechanism to use when dealing with groups of similar items because you can loop through them very easily as in the example from Arrch

Arrch:
What gave you that impression?

option a. limited intellectual capacity
option b. henry westons vintage cider (8.2%)
option c. all of the above.

thanks though, i was sure it easy... but as usual until you see it, it isnt :slight_smile:

Arrays with strings in them can be remarkably wasteful if you have a lot of short strings and few long ones.

But numerical arrays are not wasteful (unless done very wrong).