#include "PString.h" int VaneValue;// raw analog value from wind vane int Direction;// translated 0 - 360 direction int CalDir;// converted value with offset applied int LastValue; #define Offset 0; byte getChecksum(char* str) { byte cs = 0; for (unsigned int n = 1; n < strlen(str) - 1; n++) { cs ^= str[n]; } return cs; } /*=== MWV - Wind Speed and Angle === * * ------------------------------------------------------------------------------ * 1 2 3 4 5 * | | | | | * $--MWV,x.x,a,x.x,a*hh * ------------------------------------------------------------------------------ * * Field Number: * * 1. Wind Angle, 0 to 360 degrees * 2. Reference, R = Relative, T = True * 3. Wind Speed * 4. Wind Speed Units, K/M/N * 5. Status, A = Data Valid * 6. Checksum * */ void printWindNmea() { char windSentence [30]; float spd = 0; byte cs; //Assemble a sentence of the various parts so that we can calculate the proper checksum PString str(windSentence, sizeof(windSentence)); str.print("$WIMWV,"); str.print(CalDir); str.print(".0,R,"); str.print(spd); str.print(",N,A*"); //calculate the checksum cs = getChecksum(windSentence); //bug - arduino prints 0x007 as 7, 0x02B as 2B, so we add it now if (cs < 0x10) str.print('0'); str.print(cs, HEX); // Assemble the final message and send it out the serial port Serial.println(windSentence); } void setup() { LastValue = 1; Serial.begin(9600); } void loop() { VaneValue = analogRead(A7); Direction = map(VaneValue, 0, 1023, 0, 360); CalDir = Direction + Offset; if(CalDir > 360) CalDir = CalDir - 360; if(CalDir < 0) CalDir = CalDir + 360; // Only update the display if change greater than 2 degrees. if(abs(CalDir - LastValue) > 2) { //Serial.print(VaneValue); Serial.print("\t\t"); //Serial.print(CalDir; Serial.print("\t\t"); LastValue = CalDir; } printWindNmea(); }