Hi all, pls help i dont find the problem.
I read rom a photodiode which is in combination with some ir leds.
Works in general but now i got some fault and i cant figure out....
here is the code:
int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int ambientIR; // variable to store the IR coming from the ambient
int obstacleIR; // variable to store the IR coming from the object // variable to store the IR values
int distance;
int do_print=1;// variable that will tell if there is an obstacle or not
void setup(){
if (do_print == 1)
Serial.begin(9600); // initializing Serial monitor
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
}
void loop(){
distance = IR_value(5); // calling the function that will read the distance and passing the "accuracy" to it
if (do_print == 1)
Serial.println(distance); //writing the read value on Serial monitor
if (do_print == 1) delay(1000);
}
int IR_value(int c)
{
int d;
for(int x=0;x<c;x++){
digitalWrite(IRemitter,LOW);
delay(3);
ambientIR = analogRead(IRpin);
digitalWrite(IRemitter,HIGH);
delay(3);
obstacleIR = analogRead(IRpin);
d = d+ (obstacleIR-ambientIR);
}
int result=(d/c);
//Serial.println(result);
return result;
}
but if i dis-comment the serial.println in the function, i recive another value for the result. i was testing on 2 different pcs, same issue.
Any one who point me tho the failure?
thx and best regards,
Rudi