-3

Please I have this Code for my Arduino dog
but it doesn't work because I don't have convereted angle to rad for cos() operation but on end I need to get angle(after cos() operation).

const int b = 200; //Lengt
const int Pin = A0; //Pot pin
int c = 0; //Variable for Angle
int d = 0; //
int e = 0; //Final side size

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

void loop() {
c = analogRead(Pin);//Read potval write to variable c
c = map(c, 0, 1023, 0, 180);//Map potval to Angle
c = cos(c)//Calculate Cosinus
d = b - (b * c);//200 - (200 * c)
e = sqrt(d)//Calcute √e
Serial.print(e)//Print out final side a
}


//Example:
//d = 200 − (200 * 0,6560)
//d = 200 − 131,2 = 68.8
//e = √68.8
//e = 8.29

Dave2e
  • 18,599
  • 18
  • 35
  • 42
OnKav
  • 1
  • 1
    Rad to degree has to do with pi, somehow :) . (pi is even a predefined constant). Usually, int calculations are preferred, but not here. cos returns a number between -1 and +1, so there are only three possible int reults; ( -1, 0, 1 ) – datafiddler Mar 09 '20 at 13:31
  • 1
    Does this answer your question? [What is the method for converting radians to degrees?](https://stackoverflow.com/questions/135909/what-is-the-method-for-converting-radians-to-degrees) – rveerd Mar 09 '20 at 13:48

2 Answers2

0

Your question is "Why?"

The answer is: Because it's defined like that in C++.

datafiddler
  • 1,590
  • 1
  • 14
  • 27
0

Because the function cos() is defined as taking an argument in radians: https://www.arduino.cc/reference/en/language/functions/trigonometry/cos/

Radians are the SI unit for angles, and most programming libraries expect angle parameters to be in radians. Most scientific calculators have a button to switch between radians and degrees (and sometimes gradians).

pommy
  • 2,347
  • 1
  • 14
  • 20
  • I rather write why becouse i cant post it can some one help me please correct my code to work – OnKav Mar 09 '20 at 14:26
  • You lose too much precision with integers in the variables c, d, e. Use floating-point ! – pommy Mar 09 '20 at 14:41