Main Site Documentation

How to Modified the Compass readings


#1

How to change the readings from a compass to ,

North,
South
West
and East.

Please advice


#2

You can read the angle value and if is in the certain range you can treat it as a North or any other direction you want.

Edit:

Something like:

N: 337.5 <= angle < 22.5
NE: 22.5 <= angle < 67.5
E: 67.5 <= angle < 112.5


#3

@ Architect - Do you have a complete code for this ?


#4

No, but it is really simple. Something like this:

        public static string GetDirection(double angle)
        {
            if (angle >= 337.5 || angle < 22.5)
                return "N";
            else if (angle >= 22.5 && angle < 67.5)
                return "NE";
            else if (angle >= 67.5 && angle < 112.5)
                return "E";
            else if (angle >= 112.5 && angle < 157.5)
                return "SE";
            else if (angle >= 157.5 && angle < 202.5)
                return "S";
            else if (angle >= 202.5 && angle < 247.5)
                return "SW";
            else if (angle >= 247.5 && angle < 292.5)
                return "W";
            else 
                return "NW";

        }

You have to check what angle value is N for your compass. The function above is for 0(360) being North. If compass returns different angle for North then you just need to rotate return values accordingly.


#5

@ Architect - does that first line work?


#6

Should be or not and?


#7

@ Justin - Thanks for proofing! You are right the first one should be OR. I will modify it.


#8

@ Architect - your most welcome, its usually me needing the proofing :slight_smile:


#9

Updated the code.