How to change input termnal in a loop

Dear Forum Members
Is this the correct code? :

r"number" defined as integers
neither “r” nor A defined as an array

for (int n=0; n < a ; n++) { // switching input terminal loop (0 to a-1)
r[n]=analogRead(A[n]); //measure the terminals (0 to a-1)
}

For the pins you could do something like this

for (int i = 0; i < 8; i++) {
 r = analogRead(A0 + i);
}

But for multiple r you need an array.

Variable names are no strings, so you either need r0… r7 declared sepeartely (making the loop a bit clumsy, since you’d need to use switch(i) case 0: or if(i == 0) to select the correct one, or int r[8] and just use an array.

1 Like

@tomkut, A0-A7 are represented by values 10-17. You could create a array of pins and cycle though that:

int analogPins[] = {A0, A1, A2, A3, A4, A5, A6, A7};

for (int n=0; n < a ; n++) { // switching input terminal loop (0 to a-1, a < 8)
r[n]=analogRead(analogPins[n]); //measure the terminals (0 to a-1)
}

His makes the code more readable and keeps the pin abstraction :slight_smile:

2 Likes