Coding problem in arduino

This coding gives the wrong result for the sin value. I get -.22. The correct result for Sin b is .60847687. even if I used the abbreviated value of b in the print out I would get closer to the correct result. Can somebody please let me know where my coding is wrong.

int d;
float b;
float n=.986301369;




void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("input value for variable d: ");
while (Serial.available() == 0) {
  }
d = Serial.parseInt();
Serial.print("d=");
Serial.println(d);

b=(n*(d-81));
Serial.print("b=");
Serial.println(b);
Serial.print("sinb=");
Serial.println(sin(b));
}

Hello petercl14

Keep in mind the sin() function is using radians.

3 Likes

Well, this is hardly self commenting:

John, 'd' gets its value input from the serial monitor. Then the value of 'b' is derived from 'n' and 'd'. I was thinking I needed to put in a data type for sin b. I used this float ((sin)(b));
I get an error input for this. 'expected ') 'before '(' token'. all the brackets seem to be there already so I don't see why i am getting this error.
Any ideas.

Are the printed values of b and d has a values that you expect?

1 Like

And what value are you using for d ? Why don't you at least tell us that? Do you expect us to read your mind?

1 Like

yes I can see that - but why should sin( .986301369 * (d-81)) be .608...

the equation is totsally obscure.

and - what value are you seeing for d when you print it?

I think he has already left the building.

33.

119 for d, and @petercl14 needs to convert the argument to the sin() function to radians.

a7

All sorted guys. Thanks for your help. There was 2 problems with my code. I was using degrees in the trig functions. Arduino works with radians as someone pointed out. It is a simple matter to convert to radians using PI. That is also the code in arduino for PI. A capital P and I.
The second problem was the ratio 360/365. Using a calculator this gives .986301369. I was getting zero from arduino. It was pointed out to me on this forum that the numbers in this ratio are treated as integers. The result is not an integer so the result you get is zero. Who would have thought of that for such a simple calculation.
This had an easy fix which would be totally unknown to most uses of arduino. You had to us 360.0/365.0 to get the result which some wise user on the forum pointed out. But even when you print out the result you only get 2 decimals places .99. If you select more decimal places in the anwer you can get at the most 7 places which is ok for most applications. Presumably any further calculations use the 7 places and not just .99.

If you want to see more decimal places, you can for example try

Serial.print(360.0 / 365.0, 6);

and you will see the result of the division to 6 decimal places.

You can also use

Serial.print(b, 4);

to see the value of b to 4 decimal places.
I think you get the idea.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.