Go Down

Topic: pls help with different value from function (Read 447 times) previous topic - next topic

ruvy

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:
Code: [Select]
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

johncc


Code: [Select]

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?


"int d;" is not guaranteed to be initialized with 0.  Not sure if that's your problem though... try "int d=0;" ?

ruvy

thx, but is not.
All Code is same, but if i have Serial.println in the function to print the value i revice 7 for Example.
If Serial.println is commentet with // i recive 125 f.e.  :smiley-roll:

johncc

Hmmm...

What if you replace
d = d+ (obstacleIR-ambientIR);

With
d = d+ 10;

Does it print 10?  With and without the Serial.println()?

ruvy

return 112 without Serial.print.
return 10 with Serial.print...

i am a little confused the last hours  :smiley-eek:

johncc

Hmmm....

and you left the
int d=0;

in there, right?

ruvy

you are right!
I was sure i was trying this also before, but now with int d=0; works.

Thx! If i come to Texas one time, i spend you a beer  :)


Go Up