We're doing a college project where we have to build a self-moving obstacle avoiding robot that shows its coordinates. We're using a NEO-M8N GPS module.
Regarding only the coordinates:
In our output (in the Serial Monitor window) the heading angle is working fine, however the latitude and longitude values are always 0.000000_0.
We can't figure out what's wrong, any help would be seriously appreciated.
Here is our code:
//---------------------MAGNET-------------------------------------
#include <Wire.h>
#define Declination -0.0375245789
#define hmc5883l_address 0x1E
//---------------------FIM----------------------------------------
#include "Ublox.h"
#define N_FLOATS 3
Ublox M8_Gps;
float gpsArray[N_FLOATS] = {0, 0, 0};
#include <SoftwareSerial.h>
//SoftwareSerial mySerial(RX,TX);
SoftwareSerial mySerial(4, 5);
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Wire.begin(12, 14); /* join i2c bus with SDA=D6 and SCL=D5 of NodeMCU */
hmc5883l_init();
}
//---------------------MAGNET--------------------------------------
void hmc5883l_init() { /* Magneto initialize function */
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x00);
Wire.write(0x70); //8 samples per measurement, 15Hz data output rate, Normal measurement
Wire.write(0xA0); //
Wire.write(0x00); //Continuous measurement mode
Wire.endTransmission();
delay(500);
}
int hmc5883l_GetHeading() {
int16_t x, y, z;
double Heading;
Wire.beginTransmission(hmc5883l_address);
Wire.write(0x03);
Wire.endTransmission();
/* Read 16 bit x,y,z value (2's complement form) */
Wire.requestFrom(hmc5883l_address, 6);
x = (((int16_t)Wire.read() << 8) | (int16_t)Wire.read());
z = (((int16_t)Wire.read() << 8) | (int16_t)Wire.read());
y = (((int16_t)Wire.read() << 8) | (int16_t)Wire.read());
Heading = atan2((double)y, (double)x) + Declination;
if (Heading > 2 * PI) /* Due to declination check for >360 degree */
Heading = Heading - 2 * PI;
if (Heading < 0) /* Check for sign */
Heading = Heading + 2 * PI;
return (Heading * 180 / PI); /* Convert into angle and return */
}
//---------------------FIM----------------------------------------
void loop()
{
//---------------------MAGNET-------------------------------------
Serial.print("Heading Angle : ");
Serial.println(hmc5883l_GetHeading());
//delay(2000);
//---------------------FIM----------------------------------------
if (!mySerial.available())
return;
while (mySerial.available())
{
char c = mySerial.read();
if (M8_Gps.encode(c))
{
//gpsArray[0] = M8_Gps.altitude;
gpsArray[0] = M8_Gps.latitude;
gpsArray[1] = M8_Gps.longitude;
gpsArray[2] = M8_Gps.sats_in_use;
}
}
for (byte i = 0; i < 3; i++)
{
if (i == 2)
{
Serial.print(gpsArray[i], 0); Serial.println("");
}
else
{
Serial.print(gpsArray[i], 6); Serial.print("_");
}
}
delay(1000);
}