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....