Problems with acos and division

Hello,

this little Programm should calculate the direction in degree based on GPS data.
The main part reading Serial and convert it to float works well.
In the function directiongrad() the distLat and distLong works correct too.
These contains the distance in meters for the Lat and Long in GPS.

But then in Line 53 the calculation is wrong.

But in the End, for every number I tried until yet, I get NotANumber as error.
Maybe someone see my mistake :slight_smile:

TY

test.ino (1.61 KB)

Just post the code - don't make people download it.

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

}

void loop() {
  char inputCharserial = 0;
  String inputstrserial;

  while (Serial.available() > 0 && inputCharserial != '\n') {
    inputCharserial = Serial.read();
    inputstrserial = inputstrserial + inputCharserial;
  }

  if (inputstrserial != "")
  {
    String inputstrserial1 = inputstrserial;
    String inputstrserial2 = inputstrserial;
    String inputstrserial3 = inputstrserial;
    String inputstrserial4 = inputstrserial;

    inputstrserial1.remove(inputstrserial1.indexOf(" "));

    inputstrserial2.remove(0, inputstrserial2.indexOf(" ") + 1);
    inputstrserial2.remove(inputstrserial2.indexOf(" "));

    inputstrserial3.remove(0, inputstrserial3.indexOf(" ") + 1);
    inputstrserial3.remove(0, inputstrserial3.indexOf(" ") + 1);
    inputstrserial3.remove(inputstrserial3.indexOf(" "));

    inputstrserial4.remove(0, inputstrserial4.indexOf(" ") + 1);
    inputstrserial4.remove(0, inputstrserial4.indexOf(" ") + 1);
    inputstrserial4.remove(0, inputstrserial4.indexOf(" ") + 1);

    float lat1 = inputstrserial1.toFloat();
    float lat2 = inputstrserial2.toFloat();
    float long1 = inputstrserial3.toFloat();
    float long2 = inputstrserial4.toFloat();

    Serial.println(directiongrad( lat1, lat2, long1, long2 ));
  }
  delay(100);
}


float directiongrad (float disgpsLat0, float disgpsLat, float disgpsLong0, float disgpsLong)
{
  float distLat = abs(disgpsLat0 - disgpsLat) * 111194.9;
  float distLong = 111194.9 * abs(disgpsLong0 - disgpsLong) * cos(radians((disgpsLat0 + disgpsLat) / 2));

  float deg = acos( (distLat / distLong) ) * 57.29577951;

  return deg;
}

Maybe GIGO applies - what values are you inputting?

  float deg = acos( (distLat / distLong) ) * 57.29577951;

What values are in distLat and distLong? What happens if distLong is 0?

When the code is so short, just post it.

Ok, fo futher I will post the code :slight_smile:

I tested it with values from 0 - 60 as input for coordinates.
As I took different values for the variables for Longtitude, Longtitude cant be zero.

If I take values like 1 2 3 4

The Latitude distance must be 111194.90m and the Longtitude Distance must be 111156.79m
That is correct :slight_smile:

But maybe float the number is too high?

(as float can be 10^38 high I think the error happens at another point)

Than you for you fast responens

But maybe float the number is too high?

Quit smoking pot around it. 8)

How difficult is it to add some Serial.print() statements to that function, and show us the output?

It would behoove you to derive your "magic numbers" in the code, along with some comments. You have two of them, 111194.9 and 57.29577951. Whatever you did on your pocket calculator, you can do in C++.

But then in Line 53 the calculation is wrong.

True. You should be using atan2(), not acos().

Ok, my problem was using acos instead of atan2 ...

Thank you for your help. :slight_smile: