HUGE deviation in Sharp IR sensor values if other pins are defined in void setup

I wrote this code for a project -

    int sensorpin = A5;           
    int val = 0;
    double avgval = 0;
    int E1 = 11;
    int brake = 12;
    char data = ' ';
    void setup()
      pinMode(E1, OUTPUT);   
      pinMode(brake, OUTPUT);
      pinMode(sensorpin, INPUT);
    void loop()
      if (Serial.available()>0){
          data =;
      val = analogRead(sensorpin);   
      avgval = average(val);
      double volt = (avgval*5)/1023;
      Serial.println (avgval);
      if (data == 'G'){
        analogWrite(E1, 255);
        digitalWrite(brake, LOW);
      if( volt > 1.5 && volt < 1.75 ){
        digitalWrite(brake, HIGH);
    double average(double adc){
      int n = 1;
      double avg = 0.00, sum = 0.00;
        sum = sum + adc;
        avg = sum/25;
      return avg;

The code works fine, except for the fact that the Sharp IR sensor (GP2Y0A710K0F) outputs values which are wrong (by a HUGE distance), ONLY if in the “void setup()” function, the commands -

pinMode(E1, OUTPUT);
pinMode(brake, OUTPUT);

are left uncommented. If I comment the above two statements, the sketch works fine.

What on earth is wrong here? I can’t make out anything.

Wiring diagram please.

My crystal ball is a little hazy, but it sounds like pin 11 or pin 12 (E1 or brake) is miswired to the Sharp IR sensor (GP2Y0A710K0F).

It would be helpful to see the wiring (both a schematic and an actual clear photo).

Also: The average(...) function does not actually average, and it results in a wrong value because adc gets added 23 times but the division is by 25. The repeated computation of avg is a waste of time as well, as is the initialization of avg. The following is untested but is expected to return the same result and be faster:

#define ANUM 23
    double average(double adc){
    return adc*((double)ANUM/(ANUM+2));