asin(x) always returns x

I'm trying to get angles from accelerometer/gyroscope and i'm recieving float numbers from that sensor via bluetooth(Serial1) between +1 and -1. I want to get angle from that numbers so i tried using asin() function and it always returns number that i put in instead of radians.

code:

#include <math.h>
double x;
double y;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}

void loop()
{
if(Serial1.available() > 0)
{
x = Serial1.parseInt();
if (x == 1)
goto w;
x = Serial1.parseFloat();
w:
y = asin(x);
Serial.println(y);
}
}

I tried to use float insted of double and all combinations of both.
y always has value of x....why?

I stopped reading at the goto. Eliminate that, and I might look at it.

asin(x) ~ x (in radians, for small x).
Same for sin(x).

aarg:
I stopped reading at the goto. Eliminate that, and I might look at it.

#include <math.h>
double x;
double y;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}

void loop()
{
if(Serial1.available() > 0)
{
x = Serial1.parseFloat();
y = asin(x);
Serial.println(y);
}
}

Here is the code without goto. Thanks for your help.

Use code tags, and tell us what "x" is.

jremington:
asin(x) ~ x (in radians, for small x).
Same for sin(x).

Sin of an angle is always between +1 and -1. arcsin uses sin of an angle and converts it in in an angle so it alwas uses small numbers. And in a manual of math.h it says that error occurs when number is between +1 and -1. How can I not use small numbers?

We know what sin() and asin() do. We don't know what your "x" is.

nogi_13:
#include <math.h>
double x;
double y;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}

void loop()
{
if(Serial1.available() > 0)
{
x = Serial1.parseFloat();
y = asin(x);
Serial.println(y);
}
}

Here is the code without goto. Thanks for your help.

No, I meant fix it, not just take it out. The structured equivalent code is:

double x;
double y;
void setup() 
{
 Serial.begin(115200);
 Serial1.begin(115200);
}

void loop() 
{
  if(Serial1.available() > 0)
 {
   x = Serial1.parseFloat(); 
   if (x != 1)
   {   
      y = asin(x);
   }
   Serial.println(y);
 }
}

It is not necessary to do #include <math.h>, the IDE includes it by default.

nogi_13:
And in a manual of math.h it says that error occurs when number is between +1 and -1.

I assume that you mean "when a number is not between +1 and -1". Also your code doesn't check that.

Try this program:

void setup() {
  int i;
  float x;
  Serial.begin(9600);
  for (i=0; i<10; i++) {
    x = i/10.;
    Serial.print("x = ");
    Serial.print(x);
    Serial.print(" asin(x) = ");
    Serial.println(asin(x));
  }
}
void loop(){
}

aarg:
I assume that you mean "when a number is not between +1 and -1". Also your code doesn't check that.

Yes i wrote it wrong.

like this?

double sin_of_angle;
double angle;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
}

void loop()
{
if(Serial1.available() > 0)
{
sin_of_angle = Serial1.parseInt();
if (sin_of_angle == 1)
{
angle = asin(sin_of_angle);
Serial.println(angle);
}

sin_of_angle = Serial1.parseFloat();
if (sin_of_angle != 1)
{
angle = asin(sin_of_angle);
Serial.println(angle);
}
}
}

nogi_13:
I don't know how to not use goto

The answer is in reply #7.
Did you perhaps mean:
if (x > -1 and x <1)?

jremington:
Try this program:

void setup() {

int i;
  float x;
  Serial.begin(9600);
  for (i=0; i<10; i++) {
    x = i/10.;
    Serial.print("x = ");
    Serial.print(x);
    Serial.print(" asin(x) = ");
    Serial.println(asin(x));
  }
}
void loop(){
}

Ooooh, thanks, I see now what you meant by small numbers, I'm getting radians now with that program, and my program. I'm just convert that to angles now. :slight_smile:

aarg:
The answer is in reply #7.
Did you perhaps mean:
if (x > -1 and x <1)?

I've edited my code, I made mistakes. Serial.parseFloat returned 0 when it should return 1, so I used Serial.parseInt() to detect 1. I got an answer to my question, thanks for your help anyway :slight_smile: .