Shortening code with for loops, help!

Hello there!

I am using a arduino uno to read sensor values from three of the same sensors, to get a value from these sensors a small function must be used. Currently i have three different functions for each sensor, which makes the quite long and ‘confusing’, my idea was to use for loops to make it all work in one function but i have had no success.

After thinking about i am not sure it is possible to do so with sensors as they each have to monitor different conditions… Or at least i am not able to wrap my head around a solution.

Here is the current code with the three different functions:

int QRE1113_Pin = 2;
int QRE1113_Pin2 = 3;
int QRE1113_Pin3 = 4;//connected to digital 2

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


void loop(){

  int QRE_Value = readQD();
  int QRE_Value2 = readQD2();
  int QRE_Value3 = readQD3();


  Serial.print(QRE_Value); 
  Serial.print(" ");
  Serial.print(QRE_Value2);
  Serial.print(" "); 
  Serial.print(QRE_Value3); 
  Serial.println();

}


int readQD(){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( QRE1113_Pin, OUTPUT );
  digitalWrite( QRE1113_Pin, HIGH );  
  delayMicroseconds(10);
  pinMode( QRE1113_Pin, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000); 
  int diff = micros() - time;

  return diff;
}
int readQD2(){
  
  pinMode( QRE1113_Pin2, OUTPUT );
  digitalWrite( QRE1113_Pin2, HIGH );  
  delayMicroseconds(10);
  pinMode( QRE1113_Pin2, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin2) == HIGH && micros() - time < 3000); 
  int diff2 = micros() - time;

  return diff2;
}

int readQD3(){
 
  pinMode( QRE1113_Pin3, OUTPUT );
  digitalWrite( QRE1113_Pin3, HIGH );  
  delayMicroseconds(10);
  pinMode( QRE1113_Pin3, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(QRE1113_Pin3) == HIGH && micros() - time < 3000); 
  int diff3 = micros() - time;

  return diff3;
}

So if anyone could help me out that would be very appreciated!

Thank you!!

So if anyone could help me out that would be very appreciated!

Well, using three different functions that differ only in the pin used, instead of using ONE function that takes a pin number does not make sense. Somehow incorporating a for loop is not going to make any more sense.

I am a jackass, cant believe i didn't think of that.

lele_gricc:
So if anyone could help me out that would be very appreciated!

byte QRE1113_Pins[] = {2,3,4};
#define NUMPINS (sizeof(QRE1113_Pins))

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


void loop(){
  for (int i=0;i<NUMPINS;i++)
  {
    int value=readQD(QRE1113_Pins[i]);
    Serial.print(value);
    Serial.print('\t');
  }
  Serial.println(); 
}


int readQD(byte pin){
  //Returns value from the QRE1113 
  //Lower numbers mean more refleacive
  //More than 3000 means nothing was reflected.
  pinMode( pin, OUTPUT );
  digitalWrite( pin, HIGH );  
  delayMicroseconds(10);
  pinMode( pin, INPUT );

  long time = micros();

  //time how long the input is HIGH, but quit after 3ms as nothing happens after that
  while (digitalRead(pin) == HIGH && micros() - time < 3000); 
  int diff = micros() - time;

  return diff;
}