How to use #include <math.h> in arduino

Greeting Sir…
My project is about power factor detector.

So, I am using cos(x) function in my program
type data that I declare x = double
I use several math work like divide, time before I use cos() function
but, there a error I can’t understand what its mean?

Arduino: 1.0.6 (Windows 7), Board: “Arduino Uno”
C:\Program Files\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC:\Program Files\Arduino\hardware\arduino\cores\arduino -IC:\Program Files\Arduino\hardware\arduino\variants\standard -IC:\Program Files\Arduino\libraries\LiquidCrystal D:\Arduino-Output\deg_test.cpp -o D:\Arduino-Output\deg_test.cpp.o

deg_test.ino: In function ‘void setup()’:
deg_test:23: error: cannot convert ‘const char*’ to ‘double’ for argument ‘1’ to ‘double cos(double)’

Please… Help me… :confused: :confused:

You make it hard to answer questions when you post your code as an image. Please try again, posting between code tags (the </> button) after using the autoformat tool (CTRL-T) of the IDE.

There are permanent articles at the top of this forum that describe this in more detail. You should read these first before posting here.

Your immediate problem is that you probably want cos(P), NOT cos(“P”).
[Notice that “P” is of type const char* as the error message says, and P is of type double as required by the cos(…) function and as also described in the error message.]

However, you are going to have other problems because:

(1) You use int arithmetic when you should have used float or double, AND
(2) You use uninitialized variables.

Perhaps you need an introductory course in programming in the C language?

Good Luck!

You have:

double P, PF;
PF = cos("P");

You don't want the quotes (which create a sting containing the letter P, and has nothing to do with the variable P.) You should have:

PF = cos(P);

Also, be aware that all of the Arduino Trig functions operate on angles given in Radians, not degrees.

@vaj4088 : sorry… that was my bad. I totally newbie here. I try to fix it
@westfw : ya I know, but I was looking in https://www.arduino.cc/en/Reference/Cos , that cos(“P”) .
I dunno, its sound like you may use cos() function in degree by doing cos(“P”)
Cz I do all math work in degree perspective. I hope I can do cos() in degree. that all

Is there alternative way?

#include <LiquidCrystal.h>
#include <math.h>

int pin = 7;
double Hz = 50;
double T = (1000/Hz);
double ang;
double P;
double PF;
double cos(double);
double duration;


LiquidCrystal lcd(12, 11, 5, 4, 10, 9);

void setup() 
{
lcd.begin(16, 2);
pinMode(pin, INPUT);
ang = 360/T;
P = duration*ang;
PF = cos("P");
}

void loop() 
{
lcd.setCursor(0, 1);
duration = pulseIn(pin, HIGH);
lcd.print(P);
lcd.setCursor(5, 2);
lcd.print(T);
lcd.setCursor(11, 3);
lcd.print(ang);
}

then error
Arduino: 1.0.6 (Windows 7), Board: “Arduino Uno”
C:\Program Files\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC:\Program Files\Arduino\hardware\arduino\cores\arduino -IC:\Program Files\Arduino\hardware\arduino\variants\standard -IC:\Program Files\Arduino\libraries\LiquidCrystal D:\Arduino-Output\deg_test.cpp -o D:\Arduino-Output\deg_test.cpp.o

deg_test.ino: In function ‘void setup()’:
deg_test:23: error: cannot convert ‘const char*’ to ‘double’ for argument ‘1’ to ‘double cos(double)’

So in my perspective, I need cos() in degree…
If it can’t, is there alternative way?

Thanks,
Best Regard

I need cos() in degree...

Dust off that HS math you probablhy slept through:

// Converts degrees to radians.

define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)

// Converts radians to degrees.

define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)

That would mean your code would end up like:

  PF = cos(degressToRadians(P));

@Westfw : Is it working ? I try to declare but I think not suitable
Sorry sir, I just don’t get it

westfw:
Dust off that HS math you probablhy slept through:
// Converts degrees to radians.
#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
// Converts radians to degrees.
#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)

That would mean your code would end up like:

  PF = cos(degressToRadians(P));
#include <LiquidCrystal.h>
#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)

int pin = 7;
double Hz = 50;
double T = (1000/Hz);
double ang;
double P;
double PF;
double duration;

LiquidCrystal lcd(12, 11, 5, 4, 10, 9);

void setup() {
  lcd.begin(16, 2);
  pinMode(pin, INPUT);
  ang = 360/T;
  P = duration*ang;
  PF = cos(degressToRadians(P));
}

void loop() {
  lcd.setCursor(0, 1);
  duration = pulseIn(pin, HIGH);
  lcd.print(P);
  lcd.setCursor(5, 2);
  lcd.print(T);
  lcd.setCursor(11, 3);
  lcd.print(ang);
}

then still error
Arduino: 1.0.6 (Windows 7), Board: “Arduino Uno”
C:\Program Files\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC:\Program Files\Arduino\hardware\arduino\cores\arduino -IC:\Program Files\Arduino\hardware\arduino\variants\standard -IC:\Program Files\Arduino\libraries\LiquidCrystal D:\Arduino-Output\deg_test.cpp -o D:\Arduino-Output\deg_test.cpp.o

deg_test.ino: In function ‘void setup()’:
deg_test:22: error: ‘degressToRadians’ was not declared in this scope

  PF = cos(degressToRadians(P));Spelling ? No #define for degreesToRadians in any case.

I used auto format, it change to "degress"

You were supposed to include the lines that had the #define in them as well...

Ninonic: I used auto format, it change to "degress"

Sorry, but I don't believe that.

but, how?

syntax of define

define constantName value

I thinking to convert my deg value to rad, so cos() don't need to modify anymore.

Is it possible sir?

but, how?

Post #4 has the #defines for degrees to radians and vice-versa

UKHeliBob: Post #4 has the #defines for degrees to radians and vice-versa

So does Arduino.h:

#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)

@westwf : Sorry Sir, I don’t understand. I had try many ways to fix, but still not work.
In early, I want the function read degrees value, and that not work,
so then, I try to make my value to radians, consider cos() read radian, but still fail.
I don’t know why it happen… Is there wrong in my calculation? or my function?

@UKHelibob : I used both of that spelling degrees/degress… and its no effect of that
@oqibipido : How I put it in my code? I tried but nothing happen

#include <LiquidCrystal.h>
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)

int pin = 7;
double Hz = 50;
double T = (1000/Hz);
const float Pi = 3.14159; 
double P;
double PF;
double duration;

LiquidCrystal lcd(12, 11, 5, 4, 10, 9);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(17, 3);
  pinMode(pin, INPUT);
  P = (duration*2*Pi)/T;
  PF = cos(P);
}

void loop() {
   lcd.setCursor(0, 0);
  duration = pulseIn(pin, HIGH);
  lcd.print(PF);
  lcd.setCursor(0, 1);
  lcd.print(P);
  lcd.setCursor(7, 0);
  lcd.print(T);
  lcd.setCursor(7, 3);
  lcd.print(duration);

no error this time, but P always zero
I already setting duration but still the same

no error this time, but P always zero

Must be something in the half the code you didn't post.

Of course it is going to be zero. You're calculating P using duration before you ever give duration a value. So at that point in the code duration is 0. Now do the math, P must be 0 is duration is 0. In the loop you finally calculate duration but you never use it to calculate a new value for P. So P is still 0 until you calculate a new value for it.

@PaulS : what do you mean? @Delta_G : How can I fix that? can you give me the code to fix?

You have the code to calculate P. What's hard about putting that into loop after you get a value for duration?

@PaulS : what do you mean?

Look at the code you posted in Reply #15. Is that ALL of your code? If so, tell me how that compiles when there is no closing brace for loop().

@Delta_G : Now I understand thanks. Its work ;D :D ;D :D

Thanks for all Member that join this thread. Thanks for Arduino forum Team that make this happen :grin: :grin: :grinning:

[u]@westfw, @Delta_G, @vaj4088, @UKHelibob, @oqibidipo, @PaulS[/u] : I'd like to put your name as appreciation for my final project. May I get your real name? please post your real name here, please... Or for privation you can email me nico.latino7@gmail.com

Thanks, Best Regard Ninonic