void check_compass(void) { loop_counter = 0; //Reset the loop counter. battery_voltage = analogRead(battIn); //Set battery voltage. i2cmux(4); //Check if the compass is responding. HWire.beginTransmission(compass_address); //Start communication with the compass. error = HWire.endTransmission(); //End the transmission and register the exit status. if (error != 0) { //Print a message when the compass did not respond. Serial1.println("Compass not responding"); } else { HWire.beginTransmission(compass_address); //open communication with HMC5883 HWire.write(0x00); HWire.write(0x10); HWire.write(0x60); HWire.write(0x00); HWire.endTransmission(); delay(250); HWire.beginTransmission(compass_address); HWire.write(0x00); HWire.write(0x11); HWire.write(0x60); HWire.write(0x01); HWire.endTransmission(); Serial1.print("Positive bias test: "); delay(10); read_data(); Serial1.print("x: "); Serial1.print(compass_x); Serial1.print(" y: "); Serial1.print(compass_y); Serial1.print(" z: "); Serial1.println(compass_z); HWire.beginTransmission(compass_address); //open communication with HMC5883 HWire.write(0x00); HWire.write(0x12); HWire.write(0x60); HWire.write(0x01); HWire.endTransmission(); Serial1.print("Negative bias test: "); delay(10); read_data(); Serial1.print("x: "); Serial1.print(compass_x); Serial1.print(" y: "); Serial1.print(compass_y); Serial1.print(" z: "); Serial1.println(compass_z); HWire.beginTransmission(compass_address); //open communication with HMC5883 HWire.write(0x00); HWire.write(0x10); HWire.write(0x60); HWire.write(0x00); HWire.endTransmission(); delay(2000); } while (data != 'q') { //Stay in this loop until the data variable data holds a q. delayMicroseconds(3700); //Wait for 4000us to simulate a 250Hz loop. if (Serial1.available() > 0) { //If serial data is available. data = Serial1.read(); //Read the incomming byte. delay(100); //Wait for any other bytes to come in. while (Serial1.available() > 0)loop_counter = Serial1.read(); //Empty the Serial buffer. } //Tell the HMC5883 where to begin reading data HWire.beginTransmission(compass_address); HWire.write(0x03); //select register 3, X MSB register HWire.endTransmission(); read_data(); loop_counter++; if (loop_counter == 125) { Serial1.print("X-axis:"); Serial1.print (compass_x); Serial1.print(" Z-axis:"); Serial1.print(compass_z); Serial1.print(" Y-axis:"); Serial1.println(compass_y); loop_counter = 0; } } loop_counter = 0; //Reset the loop counter. print_intro(); //Print the intro to the serial monitor. } void read_data() { HWire.requestFrom(compass_address, 6); compass_x = (HWire.read() << 8) | HWire.read(); compass_z = (HWire.read() << 8) | HWire.read(); compass_y = (HWire.read() << 8) | HWire.read(); }