#include <Wire.h> #include <DFRobot_QMC5883.h> DFRobot_QMC5883 compass; float x = 0; void setup() { Serial.begin(9600); while (!compass.begin()){ Serial.println(" "); delay(500); } if(compass.isHMC() ){ Serial.println(" "); compass.setRange(HMC5883L_RANGE_1_3GA); compass.setMeasurementMode(HMC5883L_CONTINOUS); compass.setDataRate(HMC5883L_DATARATE_15HZ); compass.setSamples(HMC5883L_SAMPLES_8); }else if(compass.isQMC()){ Serial.println(" "); compass.setRange(QMC5883_RANGE_2GA); compass.setMeasurementMode(QMC5883_CONTINOUS); compass.setDataRate(QMC5883_DATARATE_50HZ); compass.setSamples(QMC5883_SAMPLES_8); } } void loop() { Vector mag = compass.readRaw(); float heading = atan2(mag.YAxis, mag.XAxis); float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / PI); heading += declinationAngle; if (heading < 0){ heading += 2 * PI; } if (heading > 2 * PI){ heading -= 2 * PI; } float headingDegrees = heading * 180/PI; x = headingDegrees; Serial.println(x); delay(300); }
Открыть пример: Online ArduBlock
Видеоурок: