-
Notifications
You must be signed in to change notification settings - Fork 5
Description
#include <Wire.h>
#include <SparkFun_MMC5983MA_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_MMC5983MA
SFE_MMC5983MA myMag;
// define the GPIO pin for the led and button
const int ledPin = 15u;
const int buttonPin = 16u;
// the setup function runs once when you press reset or power the board
void setup() {
// program button to wait for button press before proceeding so we can get the serial monitor ready
while (digitalRead(buttonPin) == HIGH) {
// Do nothing
}
Serial.begin(115200);
Serial.println("MMC5983MA Example");
Wire.begin();
if (myMag.begin() == false)
{
Serial.println("MMC5983MA did not respond - check your wiring. Freezing.");
while (true)
;
}
myMag.softReset();
Serial.println("MMC5983MA connected");
int celsius = myMag.getTemperature();
float fahrenheit = (celsius * 9.0f / 5.0f) + 32.0f;
Serial.print("Die temperature: ");
Serial.print(celsius);
Serial.print("°C or ");
Serial.print(fahrenheit, 0);
Serial.println("°F.");
// initialize digital pin ledPin as an output and buttonPin as input.
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
delay(1000);
}
// the loop function runs over and over again forever
void loop() {
// Wait for button press
while (digitalRead(buttonPin) == HIGH) {
// Do nothing
}
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
// delay(1000); // wait for a second
Serial.print("raw X");
Serial.print("\traw Y");
Serial.print("\traw Z");
Serial.print("\tX (nT)");
Serial.print("\tY (nT)");
Serial.print("\tZ (nT)");
Serial.println("\tMag (nT)");
uint32_t currentX = 0;
uint32_t currentY = 0;
uint32_t currentZ = 0;
double normalizedX = 0;
double normalizedY = 0;
double normalizedZ = 0;
// This reads the X, Y and Z channels consecutively
// (Useful if you have one or more channels disabled)
//currentX = myMag.getMeasurementX();
//currentY = myMag.getMeasurementY();
//currentZ = myMag.getMeasurementZ();
// Or, we could read all three simultaneously
myMag.performSetOperation();
delay(5);
myMag.getMeasurementXYZ(¤tX, ¤tY, ¤tZ);
Serial.print(currentX);
Serial.print("\t");
Serial.print(currentY);
Serial.print("\t");
Serial.print(currentZ);
normalizedX = (double)currentX - 131072.0;
normalizedX /= 131072.0;
normalizedY = (double)currentY - 131072.0;
normalizedY /= 131072.0;
normalizedZ = (double)currentZ - 131072.0;
normalizedZ /= 131072.0;
// The magnetometer full scale is +/- 8 Gauss (800000 nT)
// Multiply the normalized values by 8 to convert to Gauss or by 800000 to nT
//Serial.print("X axis field (nT): ");
//Serial.print("\tY axis field (nT): ");
//Serial.println("\tZ axis field (nT): ");
Serial.print("\t");
Serial.print(normalizedX * 800000, 0); // Print with 5 decimal places
Serial.print("\t");
Serial.print(normalizedY * 800000, 0);
Serial.print("\t");
Serial.print(normalizedZ * 800000, 0);
//Serial.print("\t");
//Serial.print(sq(normalizedX), 5);
//Serial.print("\t");
//Serial.print(sq(normalizedY), 5);
//Serial.print("\t");
//Serial.print(sq(normalizedZ), 5);
Serial.print("\t");
Serial.print(sqrt(sq(normalizedX)+sq(normalizedY)+sq(normalizedZ)) * 800000, 0);
Serial.println();
delay(10);
// Wait for button press
while (digitalRead(buttonPin) == HIGH) {
// Do nothing
}
myMag.performResetOperation();
delay(5);
myMag.getMeasurementXYZ(¤tX, ¤tY, ¤tZ);
Serial.print(currentX);
Serial.print("\t");
Serial.print(currentY);
Serial.print("\t");
Serial.print(currentZ);
// The magnetic field values are 18-bit unsigned. The zero (mid) point is 2^17 (131072).
// Normalize each field to +/- 1.0
normalizedX = (double)currentX - 131072.0;
normalizedX /= 131072.0;
normalizedY = (double)currentY - 131072.0;
normalizedY /= 131072.0;
normalizedZ = (double)currentZ - 131072.0;
normalizedZ /= 131072.0;
// The magnetometer full scale is +/- 8 Gauss (800000 nT)
// Multiply the normalized values by 8 to convert to Gauss or by 800000 to nT
//Serial.print("X axis field (nT): ");
//Serial.print("\tY axis field (nT): ");
//Serial.println("\tZ axis field (nT): ");
Serial.print("\t");
Serial.print(normalizedX * 800000, 0); // Print with 5 decimal places
Serial.print("\t");
Serial.print(normalizedY * 800000, 0);
Serial.print("\t");
Serial.print(normalizedZ * 800000, 0);
//Serial.print("\t");
//Serial.print(sq(normalizedX), 5);
//Serial.print("\t");
//Serial.print(sq(normalizedY), 5);
//Serial.print("\t");
//Serial.print(sq(normalizedZ), 5);
Serial.print("\t");
Serial.print(sqrt(sq(normalizedX)+sq(normalizedY)+sq(normalizedZ)) * 800000, 0);
Serial.println();
delay(10);
}