0

So I'm facing this problem a lot in my code where the program is not getting into the if else loop properly and just returning the same answer again and again.

void loop() {

double X_value = ((double)(analogRead(X_pin)-487))/540.00;
double Y_value = ((double)(529-analogRead(Y_pin)))/540.00;

double theta_rad = atan((double)Y_value/(double)X_value);
double theta_deg = ((double) theta_rad)*180.00/3.1416;

double temp = (double)abs(tan((double)theta_rad));
if (temp == 0.00) {
temp2 = 0.00;
} else {  
  double temp2 = min(temp,1.00/temp);
}

Serial.print("Theta = ");
Serial.print(theta_deg);
Serial.print("\n");
Serial.print("temp 2 = ");
Serial.print(temp2);
Serial.print("\n");

delay(1000);

}

The answer keeps on coming 0 even when temp is non zero.

Screenshot of result

If anyone knows the reason please help...

  • 6
    Please edit your question to show actual code used. What's there now has a compile error with temp2 not declared before use. Note, the temp2 declaration shown in the else branch has scope only within the else branch and is not available outside it. – James Waldby - jwpat7 Jun 17 '17 at 20:56
  • Note that You may want to use atan2() instead of atan(). – Edgar Bonet Jun 17 '17 at 21:19
  • 2
    And also it's not a good idea to compare floats for equality, as they are not "exact" numbers. For example 0.00 can be anything between -0.0049999.... and 0.0049999..... (roughly) – KIIV Jun 17 '17 at 22:03
  • The initialisation code is missing. Where is temp2 declared? and X-pin and Y_pin – Greenonline Jun 17 '17 at 22:56
  • You use doubles for everything and then use a value of 3.1416 for PI? 1) in arduino doubles are only 32 bit floats not true 64 bit doubles so the constant casting to double is unnecessary. 2) a 32 bit float is accurate to more than 5 figures so that Pi value is the limiting factor in your accuracy. – Andrew Jun 19 '17 at 9:31
1

I'm assuming that temp2 must be defined as a global or you'd be getting compiler errors.
You are re-defining temp2 within the else statement, that redefined value is then only local to that else and is lost as soon as you exit it.

void loop() {

double X_value = ((double)(analogRead(X_pin)-487))/540.00;
double Y_value = ((double)(529-analogRead(Y_pin)))/540.00;

double theta_rad = atan((double)Y_value/(double)X_value);
double theta_deg = ((double) theta_rad)*180.00/3.1416;

double temp = (double)abs(tan((double)theta_rad));

if (temp == 0.00) {
  temp2 = 0.00;
} else {  

/* Bug here.                                                             */
/* this temp2 is local to these {} and is lost as soon as we reach the } */
/* at the end of the else.                                               */
/* Remove the word double and it should work                             */

  double temp2 = min(temp,1.00/temp);
}

Serial.print("Theta = ");
Serial.print(theta_deg);
Serial.print("\n");
Serial.print("temp 2 = ");
Serial.print(temp2);
Serial.print("\n");

delay(1000);

}

Personally I'd replace the entire if ... else with:

double temp = abs(tan(theta_rad)); // removed redundant casts to double

double temp2;
if (temp > 1.0) // 1/temp will be smaller if temp > 1
  temp2 = 1.0/temp;
else
  temp2 = temp;

It'll have exactly the same effect but avoids unnecessary floating point calculations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.