Newish need helping making a function

Hello hello. So i’m making myself a basic program that utilizes the Haversign formula to calculate a heading based off of two points of longitude and latitude. Currently this project is a test and i’m inputting data via serial monitor. What I have are 4 global float variables to store these points in, and eventually i’ll create the code with the actual formula for haversign but i’m just not there yet. Right now i have some code structure in loop doing the serial grab and storing the values. You’ll notice i’m essentially doing the same group of code 4 times (one for each point) and it has me wondering if there’s a better way to do this and include it as a function and call it 4 times.

More or less, i’m wondering what the PROPER way to turn this into a function would be. This is not practical and i’m using it for learning purposes. Here’s the entire code segment.

float LongA = 0.0;
float LongB = 0.0;
float LattA = 0.0;
float LattB = 0.0;


// the setup function runs once when you press reset or power the board
void setup() {
  
 while(!Serial){} 
 Serial.begin(9600);
}


// the loop function runs over and over again forever
void loop(){



 if (LongA == 0.0){
        Serial.print("Please enter Longitude for Point A: ");
 }
 
 while (LongA == 0.0){
       
        if (Serial.available() > 0){
        LongA = Serial.parseFloat();
        }
        if (LongA != 0.0){
          Serial.println(LongA, 6);
        }
 }


if (LattA == 0.0){
        Serial.print("Please enter Lattitude for Point A: ");
 }
 
 while (LattA == 0.0){
       
        if (Serial.available() > 0){
        LattA = Serial.parseFloat();
        }
        if (LattA != 0.0){
          Serial.println(LattA, 6);
        }
 }


if (LongB == 0.0){
        Serial.print("Please enter Longitude for Point B: ");
 }
 
 while (LongB == 0.0){
       
        if (Serial.available() > 0){
        LongB = Serial.parseFloat();
        }
        if (LongB != 0.0){
          Serial.println(LongB, 6);
        }
 }

 
if (LattB == 0.0){
        Serial.print("Please enter Lattitude for Point B: ");
 }
 
 while (LattB == 0.0){
       
        if (Serial.available() > 0){
        LattB = Serial.parseFloat();
        }
        if (LattB != 0.0){
          Serial.println(LattB, 6);
        }
 }

}



void readMe(point)

{





  
}

p.s. It would be cool to be able to modify the serial prints i’m doing to reflect which point i’m recording as well, so even those print statements could lie within the function, but this is far beyond my comprehension right now.

Thanks in advance!!!

Look here

Alright. I posted this in the wrong sub-forum and i don't see a way to delete my own post...? And then I had to wait 5 minutes just to post this -_-

Moderator please move this to project guidance.

Does an array come into play here at all with this perhaps?

void setup() {
 while(!Serial){} 
 Serial.begin(9600);
 Serial.read();
}

void loop() {


Serial.print("Enter the Latitude for point A: ");
float LatA = calcPoint();
Serial.print("Enter the Longitude for point A: ");
float LongA = calcPoint();
Serial.print("Enter the Latitude for point B: ");
float LatB = calcPoint();
Serial.print("Enter the Longitude for point B: ");
float LongB = calcPoint();
Serial.print("Your bearing is: ");
float bearing = calcBearing(LatA, LongA, LatB, LongB);
Serial.println(bearing, 6);

}

float calcPoint(){
    while (Serial.available()==0) {} 
      float input = Serial.parseFloat();
      Serial.println(input, 6);
      return (input);
}

float calcBearing(float radLa, float radOa, float radLb, float radOb){

          radLa = DEG_TO_RAD * radLa;
          radOa = DEG_TO_RAD * radOa;
          radLb = DEG_TO_RAD * radLb;
          radOb = DEG_TO_RAD * radOb;
          float x = cos(radLb) * sin(abs(radOb-radOa));
          float y = cos(radLa) * sin(radLb) - sin(radLa) * cos(radLb) * cos(abs(radOa-radOb));
          float result = atan2(x,y) * RAD_TO_DEG;
          return(result);   

  
}

Well I did some heavy reading and came up with this. Using the functions i'm able to successfully do the exact same thing with a lot less code.

Is there anything else anyone can think i can clean up?

Also my value should sometimes come out as a negative value and i notice it isn't. Am I maybe doing something wrong in my definitions or math that might cause me to lose the negative value?

Iv’e double checked this formula using something basic like excel and it returns negative when it should. Anyone have any ideas here for maybe my declarations being wrong for returning or calculating a negative float?

Why don't you post your input values and the results you dislike?

TolpuddleSartre:
Why don’t you post your input values and the results you dislike?

Now that I can do!!!

If you journey over to this link

https://www.igismap.com/map-tool/bearing-angle

You can set any two points and get a heading. I picked a random point for this example (tested multiple points). Milwaukee, Wisconsin to the middle of Kazakhstan. Here are the values

Also using anymore than 6 places to the right of the decimal (on serial input) gives you a goofy very small decimal value, so for now truncate to something that looks like what i have here:

Lat A = 49.077478

Long A = 67.162588

Lat B = 42.799418

Long B = -88.140145

Your bearing should end up being -17.907 Degrees. Testing this again in excel, it looks like it may indeed be my math though i could have sworn i tested this multiple times. If anyone has any pointers i’m open to hearing them :frowning: I will post the formula below:

βearing = atan2(X,Y)

X = cos θb * sin ∆L

Y = cos θa * sin θb – sin θa * cos θb * cos ∆L

I believe i’m doing everything correctly except perhaps the deltaL. My take on deltaL is the absolute difference between the two longitudes. I did remove the absolute to test this, it made no difference as the delta of L wasn’t yielding a negative value anyways.

bump