int VaneValue;// raw analog value from wind vane
int Direction;// translated 0 – 360 direction
int CalDirection;// converted value with offset applied
int LastValue;
#define Offset 0;
void setup() {
LastValue = 1;
Serial.begin(19200);
Serial.println(“Vane Value\tDirection\tHeading”);
}
void loop() {
VaneValue = analogRead(A1);
Direction = map(VaneValue, 0, 1023, 0, 360);
CalDirection = Direction + Offset;
if(CalDirection > 360)
CalDirection = CalDirection – 360;
if(CalDirection < 0)
CalDirection = CalDirection + 360;
// Only update the display if change greater than 2 degrees.
if(abs(CalDirection – LastValue) > 5)
{
Serial.print(VaneValue); Serial.print(“\t\t”);
Serial.print(CalDirection); Serial.print(“\t\t”);
getHeading(CalDirection);
LastValue = CalDirection;
}
delay(100);
}
// Converts compass direction to heading
void getHeading(int direction) {
if (direction >= 52 && direction <= 53) {
Serial.println(“SSE”);
}
else if (direction >= 58 && direction <=61) {
Serial.println(“SE”);
}
else if (direction >= 56 && direction <= 57) {
Serial.println(“SEE”);
}
else if (direction >= 86 && direction <=92) {
Serial.println(“S”);
}
else if (direction >= 68 && direction <=70) {
Serial.println(“SSW”);
}
else if (direction >= 110 && direction <=123) {
Serial.println(“SW”);
}
else if (direction >= 104 && direction <=106) {
Serial.println(“SWW”);
}
else if (direction >= 172 && direction <=176) {
Serial.println(“E”);
}
else if (direction >= 154 && direction <=156) {
Serial.println(“NEE”);
}
else if (direction >= 278 && direction <=280) {
Serial.println(“NE”);
}
else if (direction >= 226 && direction <=230){
Serial.println(“W”);
}
else if (direction >= 251 && direction <= 253) {
Serial.println(“NNE”);
}
else if (direction >= 312 && direction <= 314){
Serial.println(“N”);
}
else if (direction >= 292 && direction <= 293){
Serial.println(“NNW”);
}
else if (direction >= 332 && direction <= 337){
Serial.println(“NW”);
}
else if (direction >= 216 && direction <= 218){
Serial.println(“NWW”);
}
else
Serial.println(“N”);
}
Ref:http://www.philpot.me/weatherinsider.html