Why is my Serial.print(.......) not printing correctly on the Serial Monitor?

When I upload my code to the Arduino, I get the wrong output for Serial.print(b,7); , on line 78, but when I comment the code below it, from line 81 - 103, I get the correct answer. The issue that I am seeing, is that the output is replaced with whatever is below it. I have made sure the baud rate is the same 9600 on the serial monitor as well as in the code.

for example
Incorrect Serial Monitor output
00:10:46.628 → Distance from point A to B, is: 0.9200225 km
00:10:46.697 → Bearing from A to B, is: -79.3392028 degrees, from North
00:10:46.735 → GPS coordinate for the midpoint is: (43.6341094,-79.3392028)
00:10:46.807 → GPS coordinate for the fraction point is: (43.6352195,-79.3413696)
00:10:46.875 → GPS coordinate for the upwind point is: (43.6307258,-79.3425064)
00:10:46.947 → GPS coordinate for the alternate upwind point is: (43.6374855,-79.3359069)

Correct Serial Monitor output
Distance from point A to B, is: 0.9200225 km
00:09:42.841 → Bearing from A to B, is: 125.2696228 degrees, from North

I am new to using these Arduino’s, anything helps, than you.

#include<math.h>
#include<SoftwareSerial.h>


float R=6371; // Radius of the Earth 

// Current GPS Position
float LATITUDE1 = 43.6365;
float LONGITUDE1 = -79.34388;
// Destination GPS Position 
float LATITUDE2 = 43.63172;
float LONGITUDE2 = -79.33455;

// Current GPS position
float a1=LATITUDE1*(PI/180); 
float b1=LONGITUDE1*(PI/180); 
// Destination GPS position 
float a2=LATITUDE2*(PI/180); 
float b2=LONGITUDE2*(PI/180); 
// Difference 
float da=(a2-a1);
float db=(b2-b1);

float d; // distance 
float b; // bearing

float mp;
float ml;
float m[2]; // midpoint

float fp;
float fl;
float f[2]; // fraction point
float fract; // fraction 

float uwpp;
float uwpl;
float uwp[2]; // upWind point

float awupp;
float awupl;
float awup[2]; // alternate upWind point



void setup() {

  d = Distance(a1,b1,a2,b2,da,db);
  b = Bearing(a1,b1,a2,b2,da,db);
  mp= MidPointLat ( a1, b1 , a2 , b2 , da , db );
  ml= MidPointLon ( a1, b1 , a2 , b2 , da , db );
  m[1]= mp;
  m[2]= ml;

  fract = 0.25;
  fp= IntermediatePointLat ( a1, b1 , a2 , b2 , da , db, fract);
  fl= IntermediatePointLon ( a1, b1 , a2 , b2 , da , db, fract);
  f[1]= fp;
  f[2]= fl;  

  uwpp= UpWindPointLat ( a1, b1 , a2 , b2 , da , db );
  uwpl= UpWindPointLon ( a1, b1 , a2 , b2 , da , db );
  uwp[1]= uwpp; 
  uwp[2]= uwpl; 

  awupp= alternateUpWindLat ( a1, b1 , a2 , b2 , da , db );
  awupl= alternateUpWindLon ( a1, b1 , a2 , b2 , da , db );
  awup[1]= awupp;
  awup[2]= awupl;
  
  Serial.begin(9600);
  while(!Serial); // NEEDED
  Serial.print(" Distance from point A to B, is: ");
  Serial.print(d,7);
  Serial.println(" km");
  
  Serial.print(" Bearing from A to B, is: ");
  Serial.print(b,7);
  Serial.println(" degrees, from North");  

  Serial.print("GPS coordinate for the midpoint is: (");
  Serial.print(m[1],7);
  Serial.print(",");
  Serial.print(m[2],7);
  Serial.println(")");

  Serial.print(" GPS coordinate for the fraction point is: (");
  Serial.print(f[1],7);
  Serial.print(",");
  Serial.print(f[2],7);
  Serial.println(")");

  Serial.print(" GPS coordinate for the upwind point is: (");
  Serial.print(uwp[1],7);
  Serial.print(",");
  Serial.print(uwp[2],7);
  Serial.println(")");

  Serial.print(" GPS coordinate for the alternate upwind point is: (");
  Serial.print(awup[1],7);
  Serial.print(",");
  Serial.print(awup[2],7);
  Serial.println(")");
  

  
  
  
}

// loop for Arduino Sensors
void loop() {
  
}

// Haversine equation 
float Distance (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference){
  float x= sin(pow(latitudeDifference/2,2))+cos(latitude1)*cos(latitude2)*sin(pow(longitudeDifference/2,2));
  float d=R*2*atan2(sqrt(x),sqrt(1-x));
  return d; // Distance between two GPS points
  }

float Bearing (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference){  
  float theta=atan2(sin(longitudeDifference)*cos(latitude2),cos(latitude1)*sin(latitude2)-sin(latitude1)*cos(latitude2)*cos(longitudeDifference));
  float b=fmod(((theta*180/PI)+360),360);
  return b; //Bearing angle
}

float MidPointLat (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference){  
  float Bx=cos(latitude2)*cos(longitudeDifference);
  float By=cos(latitude2)*sin(longitudeDifference);
  float am=atan2(sin(latitude1)+sin(latitude2),sqrt(pow(cos(latitude1)+Bx,2)+pow(By,2)))*(180/PI); //Latitude
  return am; // GPS coordinates for the Midpoint
}

float MidPointLon (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference){  
  float Bx=cos(latitude2)*cos(longitudeDifference);
  float By=cos(latitude2)*sin(longitudeDifference);
  float bm=(longitude1+atan2(By,cos(latitude1)+Bx))*(180/PI); // Longitude
  return bm; // GPS coordinates for the Midpoint
}

float IntermediatePointLat (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference, float fraction){  // f = fraction of the route ie. 1/2, 1/4, 1/8, 1/16 ......
  float c=Distance(latitude1, longitude1, latitude2, longitude2, latitudeDifference, longitudeDifference);
  float delta=c/R;
  float a=sin(1-fraction)*delta/sin(delta);
  float b=sin(fraction*delta)/sin(delta);
  float x=a*cos(latitude1)*cos(longitude1)+b*cos(latitude2)*cos(longitude2);
  float y=a*cos(latitude1)*sin(longitude1)+b*cos(latitude2)*sin(longitude2);
  float z=a*sin(latitude1)+b*sin(latitude2);
  float af=atan2(z,sqrt(pow(x,2)+pow(y,2)))*(180/PI);
  return af;
}

float IntermediatePointLon (float latitude1, float longitude1, float latitude2, float longitude2, float latitudeDifference, float longitudeDifference, float fraction){  // f = fraction of the route ie. 1/2, 1/4, 1/8, 1/16 ......
  float c=Distance(latitude1, longitude1, latitude2, longitude2, latitudeDifference, longitudeDifference);
  float delta=c/R;
  float a=sin(1-fraction)*delta/sin(delta);
  float b=sin(fraction*delta)/sin(delta);
  float x=a*cos(latitude1)*cos(longitude1)+b*cos(latitude2)*cos(longitude2);
  float y=a*cos(latitude1)*sin(longitude1)+b*cos(latitude2)*sin(longitude2);
  float bf=atan2(y,x)*(180/PI);
  return bf;
}
                  // latitude1 .longitude2 .latitude2 .Longitude2 delta lat . delta long
float UpWindPointLat (float A_1, float B_1, float A_2, float B_2, float dA, float dB){ 
  float ae=Distance(A_1, B_1, A_2, B_2, dA, dB);
  float be=Bearing ( A_1, B_1, A_2, B_2, dA, dB);
  float a=sqrt(2*pow((ae/2),2));
  float b=(be+45)*(PI/180);
  float delta = a/R;
  float aus=asin(sin(A_1)*cos(delta)+cos(A_1)*sin(delta)*cos(b))*(180/PI); // Latitude GPS coordinate 
  return aus; // GPS coordinates for the Upwind case 
}

float UpWindPointLon (float A_1, float B_1, float A_2, float B_2, float dA, float dB){ 
  float ae=Distance(A_1, B_1, A_2, B_2, dA, dB);
  float be=Bearing ( A_1, B_1, A_2, B_2, dA, dB);
  float a=sqrt(2*pow((ae/2),2));
  float b=(be+45)*(PI/180);
  float delta = a/R;
  float bus=(B_1+atan2(sin(b)*sin(delta)*cos(A_1),cos(delta)-sin(A_1)*sin(A_2)))*(180/PI); // Longitude GPS coordinate 
  return bus; // GPS coordinates for the Upwind case 
}

float alternateUpWindLat (float A_1, float B_1, float A_2, float B_2, float dA, float dB){ 
  float ae=Distance(A_1, B_1, A_2, B_2, dA, dB);
  float be=Bearing ( A_1, B_1, A_2, B_2, dA, dB);
  float a=sqrt(2*pow((ae/2),2));
  float b=(be-45)*(PI/180);
  float delta = a/R;
  float aus=asin(sin(A_1)*cos(delta)+cos(A_1)*sin(delta)*cos(b))*(180/PI); // Latitude GPS coordinate 
  return aus; // GPS coordinates for the Upwind case  
}

float alternateUpWindLon (float A_1, float B_1, float A_2, float B_2, float dA, float dB){ 
  float ae=Distance(A_1, B_1, A_2, B_2, dA, dB);
  float be=Bearing ( A_1, B_1, A_2, B_2, dA, dB);
  float a=sqrt(2*pow((ae/2),2));
  float b=(be-45)*(PI/180);
  float delta = a/R;
  float bus=(B_1+atan2(sin(b)*sin(delta)*cos(A_1),cos(delta)-sin(A_1)*sin(A_2)))*(180/PI); // Longitude GPS coordinate 
  return bus; // GPS coordinates for the Upwind case  
}

Tested on a Arduino Uno with Arduino IDE 1.8.13 and confirmed.

In the ‘C’ and ‘C++’ language, an array starts at zero.

int myArray[3] has three elements, myArray[0 ], myArray[1], myArray[2].

In your sketch, you use m[2], f[2], uwp[2] and awup[2]. You don’t own those memory locations, they belong to something else.

OH MY GOD! thanks for that catch!

I didnt realize that, rookie mistake, thank you.

i fixed it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.