Analog PINS reading IR sensors giving me strange data... not sure why???

Its 5am and I am still pulling my hair out trying to figure this one out....
I have two ir sensors that measure reflectivity. when the analog values reach a mark, they each trigger a tiny reed relay.

The problem is that I first run a calibration routine to read the analog data and figure out the range of values that each sensor is returning under current lighting. Off of that data I then set limits and triggered the relays based on changes from the midpoint. This seems to work fine, but then I get into the main loop portion of the program after the limits are established... suddenly all of the values that are being collected are in a completely different range and don't match my calibration data in any way????
I cant figure it out.

Hopefully one of you guys can help

Here is my code.... it was a lot cleaner but I was troubleshooting it got a little muddy

const int buttonPinR = 7; //pin triggering reed relay 1
const int buttonPinL = 8; //pin triggering reed relay 1
const int ledPinR =  3;  //using 40ma pin to illuminate IR LED #1 constantly on
const int ledPinL =  4; //using 40ma pin to illuminate IR LED #2 constantly on
const int eyeReadR =  A3; //IR sensor #1 
const int eyeReadL =  A4; //ir sensor#2
const int buzzerpin=13; //not implemented yet
int buzzerstate;//not implemented yet
int eyeRhighrange;//Highest value sensor picks up during calibration (right eye)
int eyeRlowrange;//Lowest value sensor picks up during calibration (right eye)
int eyeLhighrange;//Highest value sensor picks up during calibration (left eye)
int eyeLlowrange;//Lowest value sensor picks up during calibration (left eye)
int currentValR;//current Rsensor value
int currentValL;//current L sensor value
int midR; //calculated midpoint of R range
int midL; //calculated midpoint of L range
int flag=1; //flag to do one shot code (could just be in setup() but moved it to debug)
long previousMillis = 0;    //not implemented yet
unsigned long currentMillis; //not implemented yet

void setup() {
 Serial.begin(9600);
  pinMode(ledPinR, OUTPUT);  
  pinMode(ledPinL, OUTPUT);    
  pinMode(buttonPinR, OUTPUT);  
  pinMode(buttonPinL, OUTPUT); 
  pinMode(buzzerpin, OUTPUT);   //not implemented yet
  digitalWrite(ledPinR, HIGH);  //IR 40ma LED ON
  digitalWrite(ledPinL, HIGH);  //IR 40ma LED ON
  delay(200); 
 
}


void loop(){
//run once to calibrate unit detects lowest and highest values during 400 cycles and calculates the midpoint
  while (flag==1){ 
//Set all limits to the initial vallue collected from sensors
   eyeRhighrange = analogRead(eyeReadR);
 eyeLhighrange = analogRead(eyeReadL);
 eyeRlowrange = eyeRhighrange;
 eyeLlowrange = eyeLhighrange; 

// 400 loops gathering data and extending limits...  to get Max and min range in each sensor
 for (int i=1; i<400; i++){   
currentValR = analogRead(A3);
 currentValL = analogRead(A4);
Serial.print("TESTTING: "); Serial.print(i); Serial.print("    ValR: "); Serial.print(currentValR);Serial.print("     ValL: "); Serial.println(currentValL);
if (currentValR> eyeRhighrange)eyeRhighrange=currentValR;
if (currentValR< eyeRlowrange)eyeRlowrange=currentValR;
if (currentValL> eyeLhighrange)eyeLhighrange=currentValL;
if (currentValL< eyeLlowrange)eyeLlowrange=currentValL;
 }

//Calculate midpoint or L & R range
midR=(eyeRhighrange+eyeRlowrange)/2;
midL=(eyeLhighrange+eyeLlowrange)/2;

Serial.println("DONE TESTING"); //debugging
Serial.print("eyeRhigh: ");Serial.print(eyeRhighrange);
Serial.print("   eyeRlow: ");Serial.println(eyeRlowrange);
Serial.print("eyeLhigh: ");Serial.print(eyeLhighrange);
Serial.print("   eyeLlow: ");Serial.println(eyeLlowrange);
//delay(5000);
flag=0;//end of one time loop was in setup but moved it here to troubleshoot
}
  
  
  //actual looping part of code 
  
  currentValR = analogRead(A3); //read sensor#1
  currentValL = analogRead(A4); //read sensor#2

  Serial.print("midR: "); Serial.print(midR);Serial.print("     midL: "); Serial.print(midL);
Serial.print("    ValR: "); Serial.print(currentValR);Serial.print("     ValL: "); Serial.println(currentValL);

//If right sensor is on and Left sensor is off trigger one reed relay
  if (currentValR < midR-25 && currentValL >midL+25){digitalWrite(3,HIGH);digitalWrite(4,LOW);Serial.println("RIGHT BLINK");}

//if left sensor is on and Right sensor is off trigger reed relay #2
   if (currentValL < midL-25 && currentValR >midR+25){digitalWrite(3,LOW);digitalWrite(4,HIGH);Serial.println("LEFT BLINK");}

//if both sensors are off then turn off both relays
     if (currentValR > midR+25 && currentValL >midL+25){digitalWrite(3,LOW);digitalWrite(4,LOW);}

}

here is the data I get:

Calibration Portion: Here are the last 6 of 400 calibration cycles along with the calibration data results:
TESTTING: 394 ValR: 240 ValL: 56
TESTTING: 395 ValR: 246 ValL: 56
TESTTING: 396 ValR: 253 ValL: 58
TESTTING: 397 ValR: 261 ValL: 59
TESTTING: 398 ValR: 267 ValL: 62
TESTTING: 399 ValR: 271 ValL: 62
DONE TESTING
eyeRhigh: 313 eyeRlow: 41
eyeLhigh: 303 eyeLlow: 49

Now it goes to the looping part of the program almost identical to the previous part ut the ddata is completely different:

midR: 177 midL: 176 ValR: 679 ValL: 612
midR: 177 midL: 176 ValR: 653 ValL: 581
midR: 177 midL: 176 ValR: 677 ValL: 610
midR: 177 midL: 176 ValR: 655 ValL: 585
midR: 177 midL: 176 ValR: 676 ValL: 607
midR: 177 midL: 176 ValR: 659 ValL: 586

the midR and MidL are calculated midpoints from my calibration results but the ValR and ValL are the new values.... suddenly they are up around 600 instead of below 300... I dont know what changed....

Which sensors?

How are they wired?

What's the power source?

Which relays?

How are they wired?

Can we see the transition data between expected and unexpected output values - anything else happen at this point in time (such as relays switching, ambient lighting changing).

The sensors are EE-SY-171
Their emitter end is running directly from the digital pins on the nano. They are rated for 50ma and the arduino pits out 40ma so I am running them without a resistor.
The collector end is common emitter... 5 volts to resistor to collector .... emmitter to ground with analog feed taken at collector.

Honestly I don't think it's the wiring as I tested each piece of the circuit and they all work.... Could it be my two or LEDs along with the relays dropping the voltage and messing with the analog values... Messing up the arduino a reference voltage?

The relays are reeds from radio shack they are 5v dc and only draw 20ma each total I could have 40+40+20+20 = 120ma being sucked out of the nano at any one time... Could this be it?

Btw the unit is powered by a 9v battery and nothing is changing as far as ambient light

fxmech:
The sensors are EE-SY-171
Their emitter end is running directly from the digital pins on the nano. They are rated for 50ma and the arduino pits out 40ma so I am running them without a resistor.

This is all wrong. An output pin doesn't put out anything, it's the load that determines how much current will be attempted to be drawn from a output pin. You may have already damaged the sensors emitter diode and/or your output pin. You should not run an arduino pin above 30ma or so, which happily happens to be the emitter current value recommended in the test conditions from the device's datasheet http://datasheet.octopart.com/EE-SY171-Omron-datasheet-10910761.pdf . So use a series current limited resistor set for 30ma and you not cause damage to your sensor's emitter diode or the arduino output pin. The 50ma emitter value is it's absolute maximum safety rating, not the rating they recommend you run it at.

The collector end is common emitter... 5 volts to resistor to collector .... emitter to ground with analog feed taken at collector.

The datasheet at the end shows a typical wiring set-up, showing a resistor on the emitter side to set the operating current, and a load resistor from ground to the emitter of the output transistor.

Honestly I don't think it's the wiring as I tested each piece of the circuit and they all work.... Could it be my two or LEDs along with the relays dropping the voltage and messing with the analog values... Messing up the arduino a reference voltage?

I honestly think it is a wiring problem, and not understanding the specifications that need to be met.

The relays are reeds from radio shack they are 5v dc and only draw 20ma each total I could have 40+40+20+20 = 120ma being sucked out of the nano at any one time... Could this be it?

Btw the unit is powered by a 9v battery and nothing is changing as far as ambient light

A small 9 volt battery is really going to struggle trying to supply 120ma plus what the sensor takes. Get a real power source properly sized for the job.

Hmmm guess your right but I thought the arduino limited the current draw to 40ma so it seemed that the internal resistance would put me there.

The use of digital pins to drive the LEDs might be a bad idea anyways ill do a resistor and run them straight from the power source... Power supplies are not an option but higher mAh batteries are...

Still hopefully someone has some ideas as to why the data changes mid program... I appreciate the electronic advice but the values change precisely at the same part of the program and by the same value increments...

fxmech:
Hmmm guess your right but I thought the arduino limited the current draw to 40ma so it seemed that the internal resistance would put me there.

Nope, the fact that more then 40ma current flowing out an output pin can damage the pin is why AVR stated the absolute safe maximum current value of 40ma. There is no automatic limiting that kicks in at 40ma or above, hence the caution that pin damage will happen at or above that value.

The use of digital pins to drive the LEDs might be a bad idea anyways ill do a resistor and run them straight from the power source... Power supplies are not an option but higher mAh batteries are...

Still hopefully someone has some ideas as to why the data changes mid program... I appreciate the electronic advice but the values change precisely at the same part of the program and by the same value increments...