Go Down

Topic: Help with a math project (Read 153 times) previous topic - next topic

kristian1402

Hey! I am currently working on a math project using the arduino, but i've run into a problem. What my code is suposed to do is take x and y coordinates and calculate them into angles. However, when the angles for the sinus go above 90 degrees, it outputs a wrong number. As far as I can tell, it takes whatever it is above the 90 degrees, and subtracts it from 90. What i want it to do is output the right number, but i've completely ran out of ideas. Can anyone help? If it doesn't make sense, sorry, but english isn't my first language
Code: [Select]
[/
#include "math.h"

int x =7;
int y =8;

float r1 =11.4;
float r2 =11.9;
float a, b, c, d, e;

const float pi =3.14;

void setup() {
Serial.begin(9600);

c=(x*x+y*y-r1*r1-r2*r2)/(2*r1*r2);
e=acos(c);
b=e*(180/pi);

d=(y*(r1+r2*cos(e))-x*r2*sin(e))/(x*(r1+r2*cos(e))+y*r2*sin(e));
a=atan(d)*(180/pi);

Serial.print("vinkel 1: ");
Serial.println(a);
Serial.print("vinkel 2: ");
Serial.print(b);
}
]

jremington

#1
Apr 21, 2017, 05:49 pm Last Edit: Apr 21, 2017, 06:43 pm by jremington
What does your your pocket calculator give for sin(120) and sin(60)?

The built-in functions like acos() and atan() produce the same result for different inputs.
It is up to you to decide how to interpret the result, but consider using atan2() rather than atan().

Go Up