I want to make NMEA 0183 output COMPASS using acceleration sensor
I want to build an angle with the following sketch and build NMEA0183 (nmea, $ HCHDG) HDG statement from the serial port.
What sort of sketch should I add?
[code]
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
void setup(){
//GY-273
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){
Serial.println(Angleread()) ; // 方位角を返す
delay(500) ; // 500ms後に繰り返す
}
int Angleread(){
int x,y,z; //triple axis data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
return atan2((x + 20) , (y + 20)*(-1)) * RAD_TO_DEG + 180;//40と20は補正値
}
You need to do more with the value returned by Angleread() than just print it.
What are you sending the NMEA sentence to? What is the COMPLETE specification for the NMEA sentence you want to send? How often should you be sending that sentence? NOT "as often as possible" as your code would do now.
To build NMEA 0183 for heading (compass direction) there are two; one for magnetic, one for true, so first you need to know which one. If you are taking from a compass, I would say magnetic. The sentence structure is,
$ - message start
HC - Talker Identifier (HC = Heading - Magnetic Compass)
HDM - Sentence Identifier (HDM = Heading - Magnetic, HDT = Heading True)
x.x - Heading in degrees
M - M = Magnetic (T = True) hh - checksum delimiter () and checksum (hh)
- message end
I would use the snprintf function, except that floating point is not supported by default on Arduino. To get around that use an integer for the heading, and after calculating the checksum, something like
Unless you always plan to be traveling on a heading of 238 degrees, you have to insert the actual heading, calculate a valid checksum, and insert that too.
The output part should be in the loop() function.
It looks to me like you need to review the basics of how an Arduino program is organized.