Hi everyone
I have a weather station I'm working on and I have got the wind direction in number by reading the adc. the numbers it gives me all relate to a direction like 235 would be north or N. What is the best way to change these numbers to there corasponding direction without lots of if statements I can then output that character(s) to the serial monitor. also the numbers fluctuate by about +- 1 value
N = 235
NE = 562
E = 0
SE = 841
S = 735
SW = 392
W = 75
NW = 132
thanks
You shold use if-else.
For example, if you want North to be between 184 and 399: and north east between 184 and 0:
String windDirection;
if(value > 184 && value < 399){
windDirection = "N";
} else if(value > 0 && value < 184){
windDirection = "NE";
}
Hi,
What are you serially outputting to, it may have the power to change your values to characters.
But I can only see 8 IF statements needed, not much coding.
What do you want displayed if value is not in range of your character assignments?
What direction sensor are you using, what is its output code?
It seems strange that E to W values differ by 75.
yet N to S differ by 500.
What is the maximum count or the output, the tolerance for East will be 0 +1=1 and 0 -1=? ? ?
Tom... 
It's not very portable, but switch/case with ellipses would work.
Hoe about a lookup table.
struct WINDDIRECTION
{
int min; // minimum value
int max; // maximum value
char *text; // text representation of wind direction
};
WINDDIRECTION wd[] =
{
{234, 236, "N"},
{561, 563, "NE"},
{ -1, 1, "E"},
{840, 842, "SE"},
{734, 736, "S"},
{391, 393, "SW"},
{74, 76, "W"},
{131, 133, "NW"},
};
/*
lookup wind direction
in: wind direction value
returns NULL if not found, else text representation
*/
char* lookupDirection(int value)
{
// loop through the 8 wind directions
for (int cnt = 0; cnt < 89; cnt++)
{
// compare value
if (value >= wd[cnt].min && value <= wd[cnt].max)
{
// return text
return wd[cnt].text;
}
}
// return NULL to indicate error
return NULL;
}
Note:
1)
The above uses a min and a max value and is hence a little expensive on memory; it might however offer a little more flexibility. If memory is important, you can use a single variable in the struct instead of min/max pair and use limits in the if statement.
2)
Not tested.