SparkFun weather sensors convert deg to compas headings

I am building a weather station using the SparkFun sensors and SparkFun weather shield. Haven't received the shield yet, so cannot experiment with the example code provided by SparkFun.
Their code displays wind direction in degrees. I want to display it in compass headings, such as 0 deg = N, 180 deg = S. What is the easiest way to do this? Their code to produce the deg output:

//Read the wind direction sensor, return heading in degrees
int get_wind_direction()
{
    unsigned int adc;

    adc = analogRead(WDIR); // get the current reading from the sensor

    // The following table is ADC readings for the wind direction sensor output, sorted from low to high.
    // Each threshold is the midpoint between adjacent headings. The output is degrees for that ADC reading.
    // Note that these are not in compass degree order! See Weather Meters datasheet for more information.

    if (adc < 380) return (113);  //ESE
    if (adc < 393) return (68);   //ENE
    if (adc < 414) return (90);   //E
    if (adc < 456) return (158);  //SSE
    if (adc < 508) return (135);  //SE 
    if (adc < 551) return (203);  //SSW
    if (adc < 615) return (180);  //S
    if (adc < 680) return (23);   //NNE
    if (adc < 746) return (45);   //NE
    if (adc < 801) return (248);  //WSW
    if (adc < 833) return (225);  //SW
    if (adc < 878) return (338);  //NNE
    if (adc < 913) return (0);    //N
    if (adc < 940) return (293);  //WNW
    if (adc < 967) return (315);  //NW
    if (adc < 990) return (270);  //W
    return (-1); // error, disconnected?
}

I added the comments giving the Compass heading for the degree value.
I do have experience coding for Arduino, but am not an expert. A link to an example would be enough to get me going in the right direction.
Thanks for any help you can provide.

This may provide some inspiration

char * headings[] = {"N", "S", "E", "W"};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int deg = 0; deg < 360; deg++)
  {
    byte quarter = deg / 90;
    Serial.print(deg);
    Serial.print("\t");
    Serial.println(headings[quarter]);
  }
}

void loop()
{
}

If I understand what your code is doing, it is reporting any degree value by quarters of 360. I need by 22.5 deg, not quarters, or 90 deg. My attempts at modifying your code are not working.
I want to translate for example, a degree value of 202.5 deg to SSW.

My attempts at modifying your code are not working.

Post your best attempt, using code tags, and explain what goes wrong.

If I understand what your code is doing, it is reporting any degree value by quarters of 360. I need by 22.5 deg, not quarters, or 90 deg.

That is exactly what my example does. The quarter number is derived by dividing by 90 (the number of degrees in a quarter)

Of course, you could divide by any number that you want to get finer resolution. I foresee some problems with the number of degrees being a float, but they can be overcome. I note that all of the numbers in the Sparkfun example are integers

To get around using floats in your mapping, you can use round on the degrees received. If you get "22.5" round it to 22 or 23, and so on. Point 5 of a degree makes no difference to the direction reading when you are looking at NNW versus N or NW. That makes your math easier.

I use that approach on my weather station, although I do the math in PHP not on the Arduino, so my code would not help you. I use a switch case block to map the integer degrees to the compass point values.

In general, if the station gives you anything between 11.25 and 33.3, call it 23 and map to NNW etc. Since Case works in a drop through way (that is, if the current test is false, then the next is evaluated) you can simply use escalating values of "is less than" as you work around the compass to find the compass point equivalent to your current degree reading.

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