Getting turning point of robot and allowing it to move straight after turning

Hi! This question I am asking may not be relevant to Arduino as the calculations and done in the Android Studio side. I am currently doing the turning part for the robot. Currently, I have calculated the angle between 2 latitude and longitude points. However, I do not know how to make the angle such that when moving forward, my angle will be between 0 to 45 degrees. As of now, the angle for moving forward can be 0, 90, 180 or 270 degrees depending on which direction I go. I get the angle from Android application which is using atan2 to calculate the angle. The codes below shows how I get the angle using atan2.

public int bearing(float lat1, float lon1, float lat2, float lon2, float previousDegree){

        double latitude1 =  Math.toRadians(lat1);
        double longitude1 = Math.toRadians(lon1);
        double latitude2 = Math.toRadians(lat2);
        double longitude2 = Math.toRadians(lon2);

        double brng = Math.atan2((latitude2-latitude1),(longitude2-longitude1));
        double atan2 = Math.toDegrees(brng);

        if (atan2 < 0.0)
        {
         atan2 += 360;
        }
        degree = (float)atan2;
        return (int)(atan2-previousDegree);
    }

The codes below are the codes for turning written in Arduino:

if (angleDouble > -45 && angleDouble < 45){ // forward
          clearAll();
          delay(30);
          Serial.println("forward");
          //forward();
          //delay(delayTime);  
        } 
        if (angleDouble > 45 && angleDouble < 135){
          clearAll();
          delay(30);
          Serial.println("turn left");
          //turnLeft(75);
          //delay(360/2180*90);  //* 90 because it makes a 90 degree turn
        }
        if (angleDouble > -135 && angleDouble < -45){ // if angle Double is negative change positive
          clearAll();
          delay(30);
          Serial.println("turn right");
          //turnRight();
          //delay(360/2180*90);  //* 90 because it makes a 90 degree turn
        }

I hope someone can give me some advice. Thank you! :slight_smile:

The bearing has NOTHING to do with any previous heading. The bearing() function should NOT get the previous heading as an input, and should NOT adjust the bearing to account for that. That is the caller's responsibility.

There is NOTHING in your snippet that shows that you called the function, which runs on some other computer, correctly. There is NOTHING that shows that you got a reasonable value back. There is NOTHING that shows that that reasonable value was passed correctly to, or read correctly by, the Arduino.

I will NOT assume that the code you didn't post is working correctly. in the absence of ANY proof.

I hope someone can give me some advice.

I'll bet you can figure out what you need to do without any more advice.