0
votes

I am aware that similar questions have been asked, but I can't seem to find the solution. I've been using this code to return a function, but it doesn't seem to work:

#include <math.h>

// Computes the bearing in degrees from the point A(a1,a2) to
// the point B(b1,b2). Note that A and B are given in terms of
// screen coordinates
double bearing(double a1, double a2, double deg, double degy) {
a1 = 37.40733;
a2 = -121.84855;
static const double TWOPI = 6.2831853071795865;
static const double RAD2DEG = 57.2957795130823209;
// if (a1 = b1 and a2 = b2) throw an error 
double theta = atan2(deg - a1, degy + a2);
if (theta < 0.0)
    theta += TWOPI;
return RAD2DEG * theta;
Serial.print(bearing);
}

I keep receiving this error message:

Arduino: 1.8.1 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\family\Documents\Arduino\GPStester\GPStester.ino: In function 'double bearing(double, double, double, double)':

GPStester:90: error: call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous

Serial.print(bearing);

note: no known conversion for argument 1 from 'double(double, double, double, double)' to 'long unsigned int'

exit status 1 call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous ambiguous

1
The Serial.print(bearing); appears to be unreachable code. - Mikel F
I don't program arduino but the error message seems to indicate the argument expected by Serial.print is a long unsigned int. You are passing a pointer to a function to it. - R Sahu
It is very confusing to say "the point B(b1,b2)" in the comment and then "deg" and "degy" in the parameters. Are those parameters the point "B" or not? The names look like they represent angles in degrees. - molbdnilo
Note: On Arduino both float and double are 4 bytes. double isn't double precision. - gre_gor

1 Answers

3
votes

There are three issues with the code that stand out to me. First is that you overwrite the first two parameters a1 and a2. Whatever is passed to the function is being lost. Second, the Serial.print(bearing) call is unreachable code, and would never be called even if it were not throwing a compiler error.

A quick internet search of "Arduino Serial print" found descriptions of a method that will take integers and floats and send an ASCII representation of the values over a serial connection. I am guessing this is the function you are using.

However, you function call is trying to pass a pointer to a function to the Serial.print() call instead of a float or integer value. If your desire is to print the results of the call to bearing out to the serial link, you should be doing so with the return value of the function, not in the function itself with a pointer to the function.

Somewhere in your code you are going to call bearing. I suspect you want it to look like this:

Serial.print(bearing(a1,a2,deg,degy));

This will make the call to bearing with the desired parameters, and then send the result to the serial port.