# Problem with return tangent of x

Hello! I'm trying to calculate an angle, knowing the lenght of two sides of a triangle. So I've used the atan function. I've download the avr-libc librairies, and I wrote this :

``````float n;
float a;
n=x/y;
double; a=atan(double (n));
Serial.print(a);

delay(1000);
``````

Of course, x and y were declared before. Everithing seems to work during the compilation, but when I look the a value on the serial monitor. But a is always equal to 0.00 rad, whatever the values of x and y are... I've tried many things, as double; tan(double (n)); ... Also, when I want to declare a and n with double, it doesn't work...

``````double n;
double a;
``````

but when I want to upload the program, it wrote: "sketch_feb27a.ino: In function 'void loop()': sketch_feb27a:50: error: 'n' was not declared in this scope sketch_feb27a:51: error: 'a' was not declared in this scope" So what can I do to caculate this angle? Thank you!!! :)

Post the entire code, not just parts of it. This way we must be guessing the other parts.

n=x/y;

What is the datatype of x and y?

Snippets aren't much help in this context. Please post a compete sketch that demonstrates the problem, and explain what output you expect from it and what you actually get.

So what can I do to caculate this angle?

Here is my full program:

#include <SoftwareSerial.h>

``````#include "Ultrasonic.h"
#include "Math.h"
#define double tan
#define tan
#define pi
Ultrasonic ultrasonicright(8); //le capteur est sur le port D7
Ultrasonic ultrasonicleft(6); //sur le port D3

void setup()
{
Serial.begin(9600);
}
void loop()
{
long l;
l=ultrasonicleft.MeasureInCentimeters();
long r;
r = ultrasonicright.MeasureInCentimeters();

long x;
x=(l*l-r*r)/100;
Serial.print(x);
Serial.print(" cm	");
long y;
y=sqrt(((r*r))+(x/2)-(x*x)-0.0625);
Serial.print(y);
Serial.print(" cm ");

float n;
float a;
n=x/y;
double; a=tan(double(x/y)); //a= tan^-1 (x/y) , exprimé en degrés (la fonction donne un angle en radians)
Serial.print(a);

delay(1000);
}
``````

I’m trying to calculate an angle, knowing the lenght of two sides of a triangle.

atan will only give a meaningful answer if the triangle is right-angled and neither of the two sides is the hypotenuse.

If x and y are integers and x < y, the result of x/y will always be zero. e.g. x=3; y=4; then x/y = 0

Pete

``````#define double tan
#define tan
#define pi
``````

What is that supposed to do?

Pete

el_supremo: ```

# define pi

``````

What is that supposed to do?

Pete
``````

Well...honestly I don't really know... I saw that on some example on the internet. But without #define double tan and #define tan it doesn't work, so I putted it... I'm not really experienced with arduino :)

``````n=x/y;
``````

The division will be performed using integer math, since only integers are involved. The resulting int will be stored in a float. This will not make the truncated fractional part come back.

``````double; a=tan(double(x/y)); //a= tan^-1 (x/y) , exprimé en degrés (la fonction donne un angle en radians)
``````

What's the double with the semicolon doing at the beginning? I think that should go away.

Was it meant to be a cast? That's not how you do a cast.

``````a=tan(double(x/y));
``````

Since you've defined tan to be nothing, this will condense down to:

``````a=(double(x/y));
``````

And since x and y are integers and x is smaller than y the answer here will be 0. And that's what you're getting.