Howdy.
I made this for testing equipment at work, its actually a few different examples of code I found with google joined together with a little bit of my own code in the middle.
This site is excellent and I wanted to put something back in.
The code is messy, Im a beginner. This is written for an Arduino Mega 2560, I
m using 2 buttons to increase and decrease the gyro output, and 2 pots to adjust the wind speed and direction output.
I think this code could be modified fairly easily to output any NMEA sentence.
int sensorPinWindSpeed = A0; // select the input pin for the potentiometer
int sensorPinWindDirection = A15;
const int buttonPin = 10; // the number of the pushbutton pin
const int buttonPin2 = 9; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
int heading = 12; // When first powered this will be the heading
void setup()
{
Serial1.begin(9600); // initialize serial port and speed, wind speed and direction
Serial.begin(4800); // initialize serial port and speed, Gyro
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input, increase heading
pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input, decrease heading
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2); // read the state of the pushbutton value:
if (buttonState == HIGH) // adds to the heading at every push
{
heading ++;
}
if (buttonState2 == HIGH) // subtracts from the heading at every push
{
heading --;
}
if(heading==359) // Max degrees is 359, when reached return to 0
{
heading = 001;
}
if (heading==0) // Min degrees is 0, when reached return to 358,
{ // Had to set the degrees off slightly
heading = 358; // or we get stuck in a loop
}
String begin = ""; // Start of an NMEA gyro telegram
if (heading < 9);
{
begin = "$HEHDT,00"; // This adds the correct amount of zero`s
} // to the sentence, should always be a
if (heading == 10) // three digit number, eg 045, 003.
{
begin = "$HEHDT,0";
}
if (heading < 98, heading > 10)
{
begin = "$HEHDT,0";
}
if (heading > 99)
{
begin = "$HEHDT,";
}
String end = ".4,T*"; // End of the telegram,
String stringOne = begin + heading + end; // Take the start, join to the heading int, and join the end
char string[50];
stringOne.toCharArray(string, 50); // break the telegram into characters
int XOR;
int i;
int c;
for (XOR = 0, i = 0; i < strlen(string); i++) // get the string/telegram length
{
c = (unsigned char)string;
if (c == '*') break; // Ignore the $ and * symbol in the telegram
if (c != '
) XOR ^= c; // XOR everything else
}
int out = Serial.print(stringOne); // Add the telegram to the serial line without checksum
Serial.println(XOR, HEX); // convert the checksum to hex, add to the telegram and transmit
delay(1000);
//WIND SECTION
int sensorValueWindSpeed = analogRead(sensorPinWindSpeed); // read the input on analog pin 0:
float voltageWindSpeed = sensorValueWindSpeed * (5.0 / 200);
int windSpeed = voltageWindSpeed;
int sensorValueWindDirection = analogRead(sensorPinWindDirection); // read the input on analog pin 0:
float voltageWindDirection = sensorValueWindDirection * (5.0 / 14.2);
int windDirection = voltageWindDirection;
String beginWind = ""; // Start of an Wind telegram
if (windDirection < 9);
{
beginWind = "$WCMWV,00"; // This adds the correct amount of zero`s
} // to the sentence, should always be a
if (windDirection == 10) // three digit number, eg 045, 003.
{
beginWind = "$WCMWV,0";
}
if (windDirection < 98, windDirection > 10)
{
beginWind = "$WCMWV,0";
}
if (windDirection > 99)
{
beginWind = "$WCMWV,";
}
String midWind = ".0,R,"; // Middle of the Wind telegram
String endWind = ".0,M,A*"; // End of the telegram,
String stringOneWind = beginWind + windDirection + midWind + windSpeed + endWind; // Take the start, join
// to the heading int, and
//join the end
char stringWind[50];
stringOneWind.toCharArray(string, 50); // break the telegram into characters
int XORWind;
int iWind;
int cWind;
for (XORWind = 0, iWind = 0; iWind < strlen(stringWind); iWind++) // get the string/telegram length
{
cWind = (unsigned char)stringWind[iWind];
if (cWind == '*') break; // Ignore the $ and * symbol in the telegram
if (cWind != '
) XORWind ^= cWind; // XOR everything else
}
int outWind = Serial1.print(stringOneWind); // Add the telegram to the serial line without checksum
Serial1.println(XORWind, HEX); // convert the checksum to hex, add to the telegram and transmit
delay(1000); // Wait 1 second and repeat
}