#include // For atan2() function #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif // I2C address of HMC5883 magnetometer #define adresa 0x1E typedef unsigned char byte; #define I2C_FREQ 100000UL // I2C bus frequency (100 kHz) #define I2C_TIMEOUT 1000 // I2C timeout (1 second) // Function to initialize I2C communication void wireBegin() { // Initialize I2C bus with specified frequency // Implementation specific to the hardware } // Function to start an I2C transmission to a specified device address void wireBeginTransmission(uint8_t address) { // Start I2C transmission to the specified device address // Implementation specific to the hardware } // Function to write data to the I2C bus void wireWrite(uint8_t data) { // Write data to the I2C bus // Implementation specific to the hardware } // Function to end an I2C transmission void wireEndTransmission() { // End I2C transmission // Implementation specific to the hardware } // Function to request data from an I2C device void wireRequestFrom(uint8_t address, uint8_t quantity) { // Request data from the specified device address // Implementation specific to the hardware } // Function to read data from the I2C bus uint8_t wireRead() { // Read data from the I2C bus // Implementation specific to the hardware return 0; } // Function to write data to a specific register of HMC5883 void writeRegister(byte reg, byte data) { // Begin I2C transmission wireBeginTransmission(adresa); // Send register address wireWrite(reg); // Send data wireWrite(data); // End I2C transmission wireEndTransmission(); } // Function to read data from HMC5883 void readData(int x, int y, int z) { // Request 6 bytes of data from HMC5883 wireBeginTransmission(adresa); wireWrite(0x03); // Start reading from register 0x03 wireEndTransmission(); wireRequestFrom(adresa, 6); // Request 6 bytes // Read 6 bytes of data if (wireAvailable() >= 6) { x = wireRead() << 8 | wireRead(); // MSB x z = wireRead() << 8 | wireRead(); // MSB z y = wireRead() << 8 | wireRead(); // MSB y } } // Declare the baud rate for serial communication #define BAUD_RATE 9600 // Function to initialize serial communication void serialBegin(long baudRate) { // Code to initialize serial communication with the specified baud rate } // Function to send data to serial port void serialPrint(const char* data) { // Code to send data to serial port } // Function to send data to serial port void serialPrintln(const char* data) { // Code to send data to serial port } void setup() { serialBegin(BAUD_RATE); wireBegin(); // Configure HMC5883 to continuously measure writeRegister(0x02, 0x00); } void loop() { int x, y, z; // Variables for x, y, z axis values // Read data from HMC5883 readData(x, y, z); // Calculate heading in degrees and add tilt angle float heading = atan2(-y, x) / M_PI * 180.0; float tiltAngle = 0.052; // Tilt angle in radians heading += tiltAngle; if (heading < 0) { heading += 360; } if (heading > 360) { heading -= 360; } // Print values serialPrint("X axis: "); serialPrintln(x); serialPrint("Y axis: "); serialPrintln(y); serialPrint("Z axis: ");