Pages: [1]   Go Down
Author Topic: pls help with different value from function  (Read 325 times)
0 Members and 1 Guest are viewing this topic.
Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;" ?
Logged

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm...

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

With
d = d+ 10;

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

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm....

and you left the
int d=0;

in there, right?
Logged

Austria
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent!
Logged

Pages: [1]   Go Up
Jump to: