calling a variable based on for loop ideration

Hello,

I am trying to write a program for my model trains.

int segments = 3;

int segmentCurrent0 = 0;
int segmentCurrent1 = 0;
int segmentCurrent2 = 0;

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

void loop() {
  checkCurrent();
}

void checkCurrent(){
  for(int segmentsLeft = 0; segmentsLeft < segments; segmentsLeft++){
    segmentCurrentX = analogRead(13 + segmentsLeft);
    Serial.println(segmentCurrentX);
  }
}

in the function checkCurrent i check the current of a rail segment. I have in this case 3 variable all for the diffrent segments. the first time the function runs it check segment 0 and it should update the var segmentCurrent0 and so on. But how do i do this?

Noah

But how do i do this?

Use an array, instead of three discrete variables. Then, the loop index will be the array index:

int segmentCurrent[3];

  for(int segmentsLeft = 0; segmentsLeft < segments; segmentsLeft++){
    segmentCurrent[segmentsLeft] = analogRead(13 + segmentsLeft);

That really helped, thanks.